Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Graph stuff

  • Loading branch information...
commit 189b6f726abb7e5b712a32b60f15b1a3406c19dd 2 parents e56852a + 475dd43
@ReitenSchnell authored
View
10 Python/Complexity/Graphs/GraphWorld.py
@@ -179,6 +179,16 @@ def spread_vertices(self):
self.spread_vertex(v, others)
others.append(v)
+def create_graph(v_count):
+ vs = [Vertex(str(i)) for i in range(1,v_count+1)]
+ graph = Graph(vs, [])
+ return graph
+
+def draw_graph(graph):
+ layout = CircleLayout(graph)
+ gw = GraphWorld()
+ gw.show_graph(graph, layout)
+ gw.mainloop()
def main(script, n='10', *args):
View
137 Python/Complexity/Graphs/graph.py
@@ -1,4 +1,4 @@
-import itertools
+from unittest import TestCase
class Vertex(object):
def __init__(self, label = ''):
@@ -88,5 +88,136 @@ def add_regular_edges(self, degree):
if not n%2 and degree%2:
self.add_edge(Edge(vertices[i], vertices[(i+n/2)%len(vertices)]))
-
-
+class VertexTests(TestCase):
+ def arrange(self):
+ self.lbl = 'lbl'
+ self.vertex = Vertex(self.lbl)
+
+ def test_label_should_be_initialised_in_constructor(self):
+ self.arrange()
+ self.assertEqual(self.lbl, self.vertex.label)
+
+ def test_check_repr(self):
+ self.arrange()
+ self.assertEqual("Vertex('lbl')", repr(self.vertex))
+
+ def test_str_should_return_the_same_as_repr(self):
+ self.arrange()
+ self.assertEqual(repr(self.vertex), str(self.vertex))
+
+class EdgeTests(TestCase):
+ def arrange(self):
+ self.vertex1 = Vertex('v1')
+ self.vertex2 = Vertex('v2')
+ self.edge = Edge(self.vertex1, self.vertex2)
+
+ def test_tuple_should_be_initialised_in_constructor(self):
+ self.arrange()
+ self.assertEqual(self.vertex1, self.edge[0])
+ self.assertEqual(self.vertex2, self.edge[1])
+
+ def test_check_repr(self):
+ self.arrange()
+ self.assertEqual("Edge(Vertex('v1'), Vertex('v2'))", repr(self.edge))
+
+ def test_str_should_return_the_same_as_repr(self):
+ self.arrange()
+ self.assertEqual(repr(self.edge), str(self.edge))
+
+class GraphTests(TestCase):
+ def arrange(self):
+ self.vs = [Vertex('v1'), Vertex('v2'), Vertex('v3')]
+ self.es = [Edge(self.vs[0], self.vs[1]), Edge(self.vs[0], self.vs[2])]
+ self.graph = Graph(self.vs, self.es)
+
+ def test_all_vertexes_should_be_added_to_dict(self):
+ self.arrange()
+ self.assertEqual(3, len(self.graph))
+ [self.assertTrue(v in self.graph) for v in self.vs]
+
+ def test_vertex1_should_contain_edge1_and_edge2(self):
+ self.arrange()
+ self.assertEqual(self.es[0], self.graph[self.vs[0]][self.vs[1]])
+ self.assertEqual(self.es[1], self.graph[self.vs[0]][self.vs[2]])
+
+ def test_vertex2_should_contain_edge1(self):
+ self.arrange()
+ self.assertEqual(self.es[0], self.graph[self.vs[1]][self.vs[0]])
+
+ def test_vertex3_should_contain_edge2(self):
+ self.arrange()
+ self.assertEqual(self.es[1], self.graph[self.vs[2]][self.vs[0]])
+
+ def test_get_edge_edge_is_found_should_return_edge(self):
+ self.arrange()
+ result = self.graph.get_edge(self.vs[0], self.vs[1])
+ self.assertEqual(self.es[0], result)
+
+ def test_get_edge_edge_not_found_should_return_None(self):
+ self.arrange()
+ result = self.graph.get_edge(self.vs[2], self.vs[1])
+ self.assertEqual(None, result)
+
+ def test_remove_edge_should_remove_edge(self):
+ self.arrange()
+ self.graph.remove_edge(self.es[0])
+ self.assertTrue(len([inner_node for node in self.graph for inner_node in self.graph[node] if self.graph[node][inner_node] == self.es[0]]) == 0)
+
+ def test_vertices_should_return_graph_vertices(self):
+ self.arrange()
+ result = self.graph.vertices()
+ self.assertEqual(self.vs, result)
+
+ def test_edges_should_return_list_of_edges(self):
+ self.arrange()
+ result = self.graph.edges()
+ self.assertEqual(self.es, result)
+
+ def test_out_vertices(self):
+ self.arrange()
+ result = self.graph.out_vertices(self.vs[0])
+ self.assertEqual([self.vs[1], self.vs[2]], result)
+
+ def test_out_edges(self):
+ self.arrange()
+ result = self.graph.out_edges(self.vs[0])
+ self.assertEqual([self.es[0], self.es[1]], result)
+
+ def test_add_all_edges(self):
+ self.arrange()
+ self.graph = Graph(self.vs, [])
+ self.graph.add_all_edges()
+ for node_from in self.graph:
+ for node_to in self.graph:
+ if node_from != node_to:
+ self.assertTrue(self.graph[node_from][node_to] == self.graph[node_to][node_from])
+
+ def test_add_regular_edges_should_raise_if_degree_equal_to_vertex_count(self):
+ self.arrange()
+ degree = len(self.vs)
+ self.assertRaises(StandardError, self.graph.add_regular_edges, degree)
+
+ def test_add_regular_edges_should_raise_if_degree_greater_than_vertex_count(self):
+ self.arrange()
+ degree = len(self.vs) + 1
+ self.assertRaises(StandardError, self.graph.add_regular_edges, degree)
+
+ def test_add_regular_edges_should_raise_if_degree_is_odd_and_vertex_count_is_odd(self):
+ self.arrange()
+ degree = len(self.vs) - 2
+ self.assertRaises(StandardError, self.graph.add_regular_edges, degree)
+
+ def setup_and_test_regular_edges(self, degree, v_count):
+ vs = [Vertex(str(i)) for i in range(1,v_count+1)]
+ self.graph = Graph(vs, [])
+ self.graph.add_regular_edges(degree)
+ self.assertFalse(any([v for v in self.graph.vertices() if len(self.graph.out_edges(v)) != degree]))
+
+ def test_add_regular_edges_degree_is_even_v_count_is_even_check_each_vertex_degree(self):
+ self.setup_and_test_regular_edges(4,10)
+
+ def test_add_regular_edges_degree_is_even_v_count_is_odd_check_each_vertex_degree(self):
+ self.setup_and_test_regular_edges(4,9)
+
+ def test_add_regular_edges_degree_is_odd_v_count_is_even_check_each_vertex_degree(self):
+ self.setup_and_test_regular_edges(5,12)
View
38 Python/Complexity/Graphs/random_graph.py
@@ -0,0 +1,38 @@
+import random
+import itertools
+from unittest import TestCase
+import GraphWorld
+from graph import Graph, Edge, Vertex
+
+class RandomGraph(Graph):
+ def add_random_edges(self, p):
+ if p >= 1:
+ raise StandardError('Probability must be in [0,1)')
+ v_list = self.vertices()
+ combinations = list(itertools.combinations(range(len(v_list)), 2))
+ for i,j in combinations:
+ if random.random() <= p:
+ self.add_edge(Edge(v_list[i], v_list[j]))
+ return float(len(self.edges()))/float(len(combinations))
+
+
+class RandomGraphTests(TestCase):
+ def setUp(self):
+ self.results = []
+ self.prob = random.random()
+
+ def build_graph(self, v_count):
+ graph = RandomGraph([Vertex(str(i)) for i in range(1, v_count +1)], [])
+ self.results.append(graph.add_random_edges(self.prob))
+
+ def test_avg_ratio_should_be_equal_to_probability(self):
+ max_count = 60
+ for i in range(3, max_count + 1):
+ self.build_graph(i)
+ avg = float(sum(self.results)) / float(len(self.results))
+ print 'Average probability = %.2f for graph probability = %.2f'%(avg, self.prob)
+ self.assertTrue(abs(avg - self.prob) <= 0.01)
+
+ def test_should_raise_when_probability_greater_than_1(self):
+ graph = RandomGraph([], [])
+ self.assertRaises(StandardError, graph.add_random_edges, 1.1)
View
149 Python/Complexity/Graphs/unit_tests.py
@@ -1,149 +0,0 @@
-from unittest import TestCase
-from GraphWorld import CircleLayout, GraphWorld
-from graph import Vertex, Edge, Graph
-
-class VertexTests(TestCase):
- def arrange(self):
- self.lbl = 'lbl'
- self.vertex = Vertex(self.lbl)
-
- def test_label_should_be_initialised_in_constructor(self):
- self.arrange()
- self.assertEqual(self.lbl, self.vertex.label)
-
- def test_check_repr(self):
- self.arrange()
- self.assertEqual("Vertex('lbl')", repr(self.vertex))
-
- def test_str_should_return_the_same_as_repr(self):
- self.arrange()
- self.assertEqual(repr(self.vertex), str(self.vertex))
-
-class EdgeTests(TestCase):
- def arrange(self):
- self.vertex1 = Vertex('v1')
- self.vertex2 = Vertex('v2')
- self.edge = Edge(self.vertex1, self.vertex2)
-
- def test_tuple_should_be_initialised_in_constructor(self):
- self.arrange()
- self.assertEqual(self.vertex1, self.edge[0])
- self.assertEqual(self.vertex2, self.edge[1])
-
- def test_check_repr(self):
- self.arrange()
- self.assertEqual("Edge(Vertex('v1'), Vertex('v2'))", repr(self.edge))
-
- def test_str_should_return_the_same_as_repr(self):
- self.arrange()
- self.assertEqual(repr(self.edge), str(self.edge))
-
-class GraphTests(TestCase):
- def arrange(self):
- self.vs = [Vertex('v1'), Vertex('v2'), Vertex('v3')]
- self.es = [Edge(self.vs[0], self.vs[1]), Edge(self.vs[0], self.vs[2])]
- self.graph = Graph(self.vs, self.es)
-
- def test_all_vertexes_should_be_added_to_dict(self):
- self.arrange()
- self.assertEqual(3, len(self.graph))
- [self.assertTrue(v in self.graph) for v in self.vs]
-
- def test_vertex1_should_contain_edge1_and_edge2(self):
- self.arrange()
- self.assertEqual(self.es[0], self.graph[self.vs[0]][self.vs[1]])
- self.assertEqual(self.es[1], self.graph[self.vs[0]][self.vs[2]])
-
- def test_vertex2_should_contain_edge1(self):
- self.arrange()
- self.assertEqual(self.es[0], self.graph[self.vs[1]][self.vs[0]])
-
- def test_vertex3_should_contain_edge2(self):
- self.arrange()
- self.assertEqual(self.es[1], self.graph[self.vs[2]][self.vs[0]])
-
- def test_get_edge_edge_is_found_should_return_edge(self):
- self.arrange()
- result = self.graph.get_edge(self.vs[0], self.vs[1])
- self.assertEqual(self.es[0], result)
-
- def test_get_edge_edge_not_found_should_return_None(self):
- self.arrange()
- result = self.graph.get_edge(self.vs[2], self.vs[1])
- self.assertEqual(None, result)
-
- def test_remove_edge_should_remove_edge(self):
- self.arrange()
- self.graph.remove_edge(self.es[0])
- self.assertTrue(len([inner_node for node in self.graph for inner_node in self.graph[node] if self.graph[node][inner_node] == self.es[0]]) == 0)
-
- def test_vertices_should_return_graph_vertices(self):
- self.arrange()
- result = self.graph.vertices()
- self.assertEqual(self.vs, result)
-
- def test_edges_should_return_list_of_edges(self):
- self.arrange()
- result = self.graph.edges()
- self.assertEqual(self.es, result)
-
- def test_out_vertices(self):
- self.arrange()
- result = self.graph.out_vertices(self.vs[0])
- self.assertEqual([self.vs[1], self.vs[2]], result)
-
- def test_out_edges(self):
- self.arrange()
- result = self.graph.out_edges(self.vs[0])
- self.assertEqual([self.es[0], self.es[1]], result)
-
- def test_add_all_edges(self):
- self.arrange()
- self.graph = Graph(self.vs, [])
- self.graph.add_all_edges()
- for node_from in self.graph:
- for node_to in self.graph:
- if node_from != node_to:
- self.assertTrue(self.graph[node_from][node_to] == self.graph[node_to][node_from])
-
- def test_add_regular_edges_should_raise_if_degree_equal_to_vertex_count(self):
- self.arrange()
- degree = len(self.vs)
- self.assertRaises(StandardError, self.graph.add_regular_edges, degree)
-
- def test_add_regular_edges_should_raise_if_degree_greater_than_vertex_count(self):
- self.arrange()
- degree = len(self.vs) + 1
- self.assertRaises(StandardError, self.graph.add_regular_edges, degree)
-
- def test_add_regular_edges_should_raise_if_degree_is_odd_and_vertex_count_is_odd(self):
- self.arrange()
- degree = len(self.vs) - 2
- self.assertRaises(StandardError, self.graph.add_regular_edges, degree)
-
- def show_graph(self):
- layout = CircleLayout(self.graph)
- gw = GraphWorld()
- gw.show_graph(self.graph, layout)
- gw.mainloop()
-
- def setup_regular_edges(self, degree, v_count):
- self.vs = [Vertex(str(i)) for i in range(1,v_count+1)]
- self.graph = Graph(self.vs, [])
- self.graph.add_regular_edges(degree)
- self.show_graph()
- self.assertFalse(any([v for v in self.graph.vertices() if len(self.graph.out_edges(v)) != degree]))
-
- def test_add_regular_edges_degree_is_even_v_count_is_even_check_each_vertex_degree(self):
- self.setup_regular_edges(4,10)
-
- def test_add_regular_edges_degree_is_even_v_count_is_odd_check_each_vertex_degree(self):
- self.setup_regular_edges(4,9)
-
- def test_add_regular_edges_degree_is_odd_v_count_is_even_check_each_vertex_degree(self):
- self.setup_regular_edges(5,12)
-
-
-
-
-
Please sign in to comment.
Something went wrong with that request. Please try again.