-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSGRAPH.py
More file actions
115 lines (99 loc) · 3.48 KB
/
Copy pathDFSGRAPH.py
File metadata and controls
115 lines (99 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#NAME : P PUJITHA SRI LAKSHMI PALADUGU
#NET ID : PXP142730
#DATE STARTED: 2/1/2014.
#PURPOSE : This is the assignment for the class CS6364
#Design an intelligent agent to draw a continuous line covering all the links from node A to node A and node A to anywhere.
#DESCRPTION :This module mainly find the path using Depth first search. The function takes input as two graphs staring node and no of edges
#it find the path starting from node A to node A and node A to anywhere.
# This function gets the graph and inserts children nodes in the stack and removes the last element from the stack and checks if there is an
#edge between two edges. if edge exists,then path list is appended. This process is repeated until all the links of graph are covered only once.
__author__ = 'pujitha'
import random
def neihgb(graph, start):
odd = [ x for x in graph.keys() if len(graph[x])&1 ]
stack=[start];
path=[]
nodes_exp=0
while stack:
v = stack[-1]
if graph[v]:
nodes_exp=nodes_exp+1
u = graph[v][0]
stack.append(u)
# deleting edge u-v
del graph[u][graph[u].index(v) ]
del graph[v][0]
else:
path.append(stack.pop())
path.append(nodes_exp)
return path
#it calls def BFS and stores the values in a list and prints the list.
def main():
graph1={ 'A':['B','C','D'],
'B':['A','D','C'],
'C':['A','B','D','E'],
'D':['A','B','C','E'],
'E':['C','D']}
print("--------------------------------------------------------------------------------------------------------------")
print ("Solution for Graph1 in DFS")
list1=list(neihgb(graph,'A'))
# print list1
# m=len(list1)-1
nodes=list1.pop()
print nodes
list2=list1[::-1]
if (list2[-1]== 'A'):
print(list2[-1])
print("path exist from source node 'A' to 'A' only"+" and Path is:")
print list2
print("Solution:")
list3=[]
list3.extend([list2[0],list2[1],nodes,nodes,nodes,list2[-1]])
print(list3)
else:
print("Path from source node to other nodes exist"+ " and Path is:")
print list2
print("Solution:")
list3=[]
list3.extend([list2[0],list2[1],nodes,nodes,nodes,list2[-1]])
print(list3)
print("no path exist from source node 'A' to 'A' ")
print("No Solution")
print("--------------------------------------------------------------------------------------------------------------")
print ("Solution for Graph2 in DFS")
graph2={'A':['B','C','D','F'],
'B':['A','C','D','F'],
'C':['A','B','D','E'],
'D':['A','B','C','E'],
'E':['C','D'],
'F':['A','B'] }
list1 = list(neihgb(graph2,'A'))
nodes=list1.pop()
list2=list1[::-1]
if (list2[-1]== 'A'):
print("path exist from source node 'A' to 'A'"+"and Path is:")
print list2
print("Solution:")
list3=[]
list3.extend([list2[0],list2[1],nodes,nodes,nodes,list2[-1]])
print(list3)
print("no path exist from source node 'A' to other nodes")
print("No solution")
else:
print("Path from source node to other nodes exit")
print list2
print("Solution:")
list3=[]
list3.extend([list2[0],list2[1],nodes,nodes,nodes,list2[-1]])
print(list3)
print("no path exist from source node 'A' to 'A'")
print("No Solution")
raw_input()
if __name__ == '__main__':
graph= { 'A':['B','C','D'],
'B':['A','D','C'],
'C':['A','B','D','E'],
'D':['A','B','C','E'],
'E':['C','D']
}
main()