Exemplo n.º 1
0
class TestGraph(unittest.TestCase):
    def setUp(self):
        self.my_graph = DirectedGraph()

    def test_init(self):
        self.assertEqual(self.my_graph.nodes, {})

    def test_addEdge(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.assertEqual(self.my_graph.nodes, {"Emil": ["Galin"], "Galin": []})

    def test_getNEighbours(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.assertEqual(self.my_graph.getNeighboursFor("Emil"), ["Galin", "Pesho"])

    def test_pathBetween_true(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.assertTrue(self.my_graph.pathBetween("Emil", "Dimitrichka"))

    def test_pathBetween_false(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.assertFalse(self.my_graph.pathBetween("Atanas", "Emil"))

    def test_pathBetween_cycle(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.my_graph.addEdge("Dimitrichka", "Emil")
        print(self.my_graph)
        self.assertTrue(self.my_graph.pathBetween("Emil", "Dimitrichka"))
Exemplo n.º 2
0
"""
    Example Single Source Shortest Path. These are drawn from
    "Algorithms in a Nutshell (1ed)", pp. 154 and 155

    Author: George Heineman    
"""
from dijkstra import *
from graph import DirectedGraph

d = DirectedGraph()
d.addEdge(0, 1, 2)
d.addEdge(0, 4, 4)
d.addEdge(1, 2, 3)
d.addEdge(2, 4, 1)
d.addEdge(2, 3, 5)
d.addEdge(3, 0, 8)
d.addEdge(4, 3, 7)

print(d)

dist = singleSourceShortest(d, 0)

print(dist)

d = DirectedGraph()
d.addEdge(0, 1, 6)
d.addEdge(0, 3, 18)
d.addEdge(0, 2, 8)
d.addEdge(1, 4, 11)
d.addEdge(4, 5, 3)
d.addEdge(2, 3, 9)
Exemplo n.º 3
0
"""
    Example Single Source Shortest Path. These are drawn from
    "Algorithms in a Nutshell (1ed)", pp. 154 and 155

    Author: George Heineman    
"""
from dijkstra import *
from graph import DirectedGraph

d = DirectedGraph()
d.addEdge(0,1,2)
d.addEdge(0,4,4)
d.addEdge(1,2,3)
d.addEdge(2,4,1)
d.addEdge(2,3,5)
d.addEdge(3,0,8)
d.addEdge(4,3,7)

print (d)

dist = singleSourceShortest(d, 0)

print (dist)

d = DirectedGraph()
d.addEdge(0,1,6)
d.addEdge(0,3,18)
d.addEdge(0,2,8)
d.addEdge(1,4,11)
d.addEdge(4,5,3)
d.addEdge(2,3,9)
Exemplo n.º 4
0
class TestGraph(unittest.TestCase):
    def setUp(self):
        self.my_graph = DirectedGraph()

    def test_init(self):
        self.assertEqual(self.my_graph.nodes, {})

    def test_addEdge(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.assertEqual(self.my_graph.nodes, {"Emil": ["Galin"], "Galin": []})

    def test_getNEighbours(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.assertEqual(self.my_graph.getNeighboursFor("Emil"),
                         ["Galin", "Pesho"])

    def test_pathBetween_true(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.assertTrue(self.my_graph.pathBetween("Emil", "Dimitrichka"))

    def test_pathBetween_false(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.assertFalse(self.my_graph.pathBetween("Atanas", "Emil"))

    def test_pathBetween_cycle(self):
        self.my_graph.addEdge("Emil", "Galin")
        self.my_graph.addEdge("Emil", "Pesho")
        self.my_graph.addEdge("Galin", "Atanas")
        self.my_graph.addEdge("Atanas", "Dimitrichka")
        self.my_graph.addEdge("Dimitrichka", "Emil")
        print(self.my_graph)
        self.assertTrue(self.my_graph.pathBetween("Emil", "Dimitrichka"))