示例#1
0
def main():


	#Initial node a
	a = Graph('a')

	#Children of a
	a.addchild(Graph('b'))
	a.addchild(Graph('d'))
	a.addchild(Graph('f'))

	#Children of b
	a.child[0].addchild(Graph('c'))
	a.child[0].addchild(a.child[2])

	#Children of c
	a.child[0].child[0].addchild(a.child[1])

	#Children of d
	a.child[1].addchild(a.child[0])

	#Children of f
	a.child[2].addchild(a.child[1])

	#e initialized seperately as it is not a child of any other node
	e = Graph('e')

	#Children of e - d and f
	e.addchild(a.child[1])
	e.addchild(a.child[2])

	#Initial dict of depth first numbers - unknown on all nodes
	nodes={'a':'inf','b':'inf','c':'inf','d':'inf','e':'inf','f':'inf'}
	nodes2={'a':'inf','b':'inf','c':'inf','d':'inf','e':'inf','f':'inf'}

	expM  = {'a':0,'b':1,'c':2,'d':3,'e':'inf','f':2}
	expN = {'a':'inf','b':2,'c':3,'d':1,'e':0,'f':3}


	print "Expected:"
	print "Depth First Search - Start Node a:"
	
	print expM

	print '\n',"Depth First Search - Start Node e:"

	print expN

	print '\n',"Resultant:"
	print "Depth First Search - Start Node a:"

	M = depthFirst(a,nodes)

	print M

	print '\n',"Depth First Search - Start Node e:"

	N = depthFirst(e,nodes2)

	print N
示例#2
0
  def test_toposort(self):
    g = Graph()
    g.add_edge('A', ['B', 'G'])
    g.add_edge('B', ['C'])
    g.add_edge('G', ['C'])
    g.add_edge('C', ['F', 'D'])
    g.add_edge('D', ['F', 'E'])
    g.add_edge('F', [])
    g.add_edge('E', [])

    expected = ['E','F','D','C','B','G','A']
    self.assertEqual(toposort(g, 'A'), expected)
示例#3
0
def graphfunction3():
    print "****************************Output for graph three********************************\n"
    f = open('input3.txt', 'r')
    node = []
    connecting_node = []
    for line in f:
        temp = line.split(" ")
        node.append(int(temp[0].strip()))
        connecting_node.append(int(temp[1].strip()))
    counter = 1
    temp = []
    nodeset = {
        1: [],
        2: [],
        3: [],
        4: [],
        5: [],
        6: [],
        7: [],
        8: [],
        9: [],
        10: []
    }
    while (counter <= 10):
        temp = []
        for i in range(0, node.__len__()):
            if (node[i] == counter):
                temp.append(connecting_node[i])
        nodeset[counter] = temp
        counter += 1
    print 'Vertex  Distance  [Path]'
    implement_bfs(nodeset)
    print nodeset

    a = Vertex('1')
    b = Vertex('2')
    c = Vertex('3')
    d = Vertex('4')
    e = Vertex('5')
    f = Vertex('6')
    g = Vertex('7')
    h = Vertex('8')
    i = Vertex('9')
    j = Vertex('10')

    # directed graph in form of vertices for keeping track of discovery and finish time of a node.
    G = Graph(
        OrderedDict([(a, [h]), (b, [a, c, g]), (c, [b, f]), (d, [b]), (e, [f]),
                     (f, []), (g, [c, h]), (h, [c, d, i]), (i, [f, h, j]),
                     (j, [a, i])]))
    G.DFS()
    G.classifyedges()
    G.toposort()
    print "**********************************************************************************\n\n\n\n"
示例#4
0
def main():

    #Initial node a
    a = Graph('a')

    #Children of a
    a.addchild(Graph('b'))
    a.addchild(Graph('d'))
    a.addchild(Graph('f'))

    #Children of b
    a.child[0].addchild(Graph('c'))
    a.child[0].addchild(a.child[2])

    #Children of c
    a.child[0].child[0].addchild(a.child[1])

    #Children of d
    a.child[1].addchild(a.child[0])

    #Children of f
    a.child[2].addchild(a.child[1])

    #e initialized seperately as it is not a child of any other node
    e = Graph('e')

    #Children of e - d and f
    e.addchild(a.child[1])
    e.addchild(a.child[2])

    #Initial dict of depth first numbers - unknown on all nodes
    nodes = {
        'a': 'inf',
        'b': 'inf',
        'c': 'inf',
        'd': 'inf',
        'e': 'inf',
        'f': 'inf'
    }
    nodes2 = {
        'a': 'inf',
        'b': 'inf',
        'c': 'inf',
        'd': 'inf',
        'e': 'inf',
        'f': 'inf'
    }

    expM = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 'inf', 'f': 2}
    expN = {'a': 'inf', 'b': 2, 'c': 3, 'd': 1, 'e': 0, 'f': 3}

    print "Expected:"
    print "Depth First Search - Start Node a:"

    print expM

    print '\n', "Depth First Search - Start Node e:"

    print expN

    print '\n', "Resultant:"
    print "Depth First Search - Start Node a:"

    M = depthFirst(a, nodes)

    print M

    print '\n', "Depth First Search - Start Node e:"

    N = depthFirst(e, nodes2)

    print N
示例#5
0
from dfs import Graph

g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)

print("Following is Depth First Traversal")
g.DFS()
print()
print("*****",
      "ABHISHANK PANDEY",
      "181500011",
      "GLA UNIVERSITY",
      "SECTION-I",
      "CLASS ROLL.NO-02",
      sep="\n")