Skip to content

Commit

Permalink
adjacency list in python
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Meno committed Jan 22, 2012
1 parent a8cf05b commit 3628378
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
26 changes: 26 additions & 0 deletions python/adjacency_list.py
Original file line number Diff line number Diff line change
@@ -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')
7 changes: 7 additions & 0 deletions python/graph1.dat
Original file line number Diff line number Diff line change
@@ -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.