Skip to content
Browse files

adjacency list in python

  • Loading branch information...
1 parent a8cf05b commit 3628378771ef0053c9b38884e9e988b39668883a @GregMeno committed Jan 22, 2012
Showing with 33 additions and 0 deletions.
  1. +26 −0 python/adjacency_list.py
  2. +7 −0 python/graph1.dat
View
26 python/adjacency_list.py
@@ -0,0 +1,26 @@
+import sys
+
+graph = {}
+
+def build_graph(graph_file):
+ with open(graph_file) as f:
+ for l in f.readlines():
+ start, end = l.strip('\n').split(',')
+ arc_list = graph.get(start, [])
+ arc_list.append(end)
+ if start not in graph:
+ graph[start] = arc_list
+
+def arc_in(graph, start, end):
+ return end in graph.get(start,[])
+
+def edge_in(graph, first, second):
+ return arc_in(graph, first, second) and arc_in(graph, second, first)
+
+if __name__ == '__main__':
+ build_graph(sys.argv[1])
+
+ print graph
+ print edge_in(graph, '1','3')
+ print edge_in(graph, '1','8')
+ print arc_in(graph, '1','8')
View
7 python/graph1.dat
@@ -0,0 +1,7 @@
+1,3
+3,1
+1,8
+2,3
+2,4
+2,6
+3,7

0 comments on commit 3628378

Please sign in to comment.
Something went wrong with that request. Please try again.