def test_is_graph_cyclic(setup_graph):
    gr = setup_graph
    assert is_graph_cyclic(gr) == True
    gr = graph()
    gr.add_nodes(["0", "1", "2", "3"])
    gr.add_edges([("0", "1"), ("1", "2"), ("2", "3")])
    assert is_graph_cyclic(gr) == False
Esempio n. 2
0
def setup_graph():
	gr = graph()
	gr.add_nodes(["s", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"])
	gr.add_edges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
	gr.add_edges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])
	gr.add_edges([("g", "h"), ("f", "g")])
	gr.add_edges([("j", "k"), ("j", "l")])
	return gr
Esempio n. 3
0
 def setUp(self):
     self.gr = graph()
     self.gr.add_nodes(["a", "b", "c", "d", "e", "f"])
     self.gr.add_edge(("a", "b"))
     self.gr.add_edge(("a", "f"))
     self.gr.add_edge(("b", "c"))
     self.gr.add_edge(("c", "e"))
     self.gr.add_edge(("c", "d"))
     self.gr.add_edge(("d", "f"))
Esempio n. 4
0
 def test_kruskals_minimum_spanning_tree(self):
     gr = graph()
     gr.add_nodes(["a", "b", "c", "d"])
     gr.add_edge(("a", "b"), 4)
     gr.add_edge(("b", "c"), 3)
     gr.add_edge(("a", "c"), 1)
     gr.add_edge(("c", "d"), 2)
     min_cost = kruskal_MST(gr)
     self.assertEqual(min_cost, 6)
Esempio n. 5
0
def test_kruskals_minimum_spanning_tree(setup_graph):
	gr = setup_graph
	gr = graph()
	gr.add_nodes(["a", "b", "c", "d"])
	gr.add_edge(("a", "b"), 4)
	gr.add_edge(("b", "c"), 3)
	gr.add_edge(("a", "c"), 1)
	gr.add_edge(("c", "d"), 2)
	min_cost = kruskal_MST(gr)
	assert min_cost == 6
def setup_graph():
    gr = graph()
    gr.add_nodes(["a", "b", "c", "d", "e", "f"])
    gr.add_edge(("a", "b"))
    gr.add_edge(("a", "f"))
    gr.add_edge(("b", "c"))
    gr.add_edge(("c", "e"))
    gr.add_edge(("c", "d"))
    gr.add_edge(("d", "f"))
    return gr
 def test_kruskals_minimum_spanning_tree(self):
     lines = [l for l in open("tests/edges.txt")]
     lines = lines[1:]
     edges = (l.split() for l in lines)
     gr = graph()
     for (u, v, w) in edges:
         if u not in gr.nodes():
             gr.add_node(u)
         if v not in gr.nodes():
             gr.add_node(v)
         gr.add_edge((u, v), int(w))
     min_cost = kruskal_MST(gr)
     self.assertEqual(min_cost, 39)
    def setUp(self):
        self.gr = graph()
        self.gr.add_nodes(["s", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"])
        self.gr.add_edges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
        self.gr.add_edges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])
        self.gr.add_edges([("g", "h"), ("f", "g")])
        self.gr.add_edges([("j", "k"), ("j", "l")])

        self.digr = digraph()
        self.digr.add_nodes(["s", "a", "b", "c", "d", "e", "f"])
        self.digr.add_edges([("s", "a"), ("a", "b"), ("b", "a"), ("c", "b")])
        self.digr.add_edges([("b", "s"), ("s", "d"), ("d", "e"), ("e", "d")])
        self.digr.add_edges([("b", "f"), ("e", "f")])
Esempio n. 9
0
    def setUp(self):
        self.gr = graph()
        self.gr.add_nodes(["s", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"])
        self.gr.add_edges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
        self.gr.add_edges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])
        self.gr.add_edges([("g", "h"), ("f", "g")])
        self.gr.add_edges([("j", "k"), ("j", "l")])

        self.digr = digraph()
        self.digr.add_nodes(['s', 'a', 'b', 'c', 'd', 'e', 'f'])
        self.digr.add_edges([("s", "a"), ("a", "b"), ("b", "a"), ("c", "b")])
        self.digr.add_edges([("b", "s"), ("s", "d"), ("d", "e"), ("e", "d")])
        self.digr.add_edges([("b", "f"), ("e", "f")])
 def test_kruskals_minimum_spanning_tree(self):
     lines = [l for l in open("tests/edges.txt")]
     lines = lines[1:]
     edges = (l.split() for l in lines)
     gr = graph()
     for (u, v, w) in edges:
         if u not in gr.nodes():
             gr.add_node(u)
         if v not in gr.nodes():
             gr.add_node(v)
         gr.add_edge((u, v), int(w))
     min_cost = kruskal_MST(gr)
     self.assertEqual(min_cost, 39)
Esempio n. 11
0
import os, sys

sys.path.append(os.path.join(os.getcwd(), os.path.pardir))
from graphs.graph import graph
from union_find.unionfind import UnionFind

lines = [l for l in open("edges.txt").readlines()]
lines = lines[1:]
edges = [l.split() for l in lines]
gr = graph()

for (u, v, w) in edges:
    if u not in gr.nodes():
        gr.add_node(u)
    if v not in gr.nodes():
        gr.add_node(v)
    gr.add_edge((u, v), int(w))


def kruskal_MST(gr):
    sorted_edges = sorted(gr.get_edge_weights())
    uf = UnionFind()
    min_cost = 0
    for (w, (u, v)) in sorted_edges:
        if (not uf.get_leader(u) and not uf.get_leader(v)) or (uf.get_leader(u) != uf.get_leader(v)):
            uf.insert(u, v)
            min_cost += w
    return min_cost


print kruskal_MST(gr)