コード例 #1
0
ファイル: __init__.py プロジェクト: vkmamidi/Graphs
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"
コード例 #2
0
ファイル: main.py プロジェクト: pabhishank25/Algorithm-DFS
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")