Skip to content

Commit 28ac55b

Browse files
Merge pull request #38 from master-fury/master
BFS (Graph Algorithm)
2 parents 4bb63a2 + 778d9cd commit 28ac55b

File tree

2 files changed

+123
-0
lines changed
  • Graph_Algorithms/src

2 files changed

+123
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#BFS by Master-Fury
2+
import collections
3+
4+
"""
5+
Wrapper function for the print function.
6+
Used as the default visitFunc for bfs
7+
"""
8+
def visitPrint(i):
9+
print(i)
10+
11+
"""
12+
A class representing a undirected graph of nodes.
13+
An edge can be added between two nodes by calling addEdge
14+
*This class assumes all edge weights are equal
15+
"""
16+
class Graph:
17+
def __init__(self):
18+
self.adjList = collections.defaultdict(set)
19+
20+
def addEdge(self, node1, node2):
21+
self.adjList[node1].add(node2)
22+
self.adjList[node2].add(node1)
23+
24+
"""
25+
Given a 'start' node and a 'graph', call visitFunc
26+
sequentially on the current node, and then its children
27+
and so forth.
28+
When visiting each node, mark it as visited by adding it to the hashmap.
29+
Then queue up all of its children to be visited next.
30+
"""
31+
def bfs(start, graph, visitFunc=visitPrint):
32+
visited = collections.defaultdict(bool)
33+
queue = collections.deque()
34+
35+
queue.append(start)
36+
37+
while(len(queue) > 0):
38+
current = queue.popleft()
39+
40+
if (not visited[current]):
41+
visited[current] = True
42+
visitFunc(current)
43+
for neighbor in graph.adjList[current]:
44+
queue.append(neighbor)
45+
46+
# Testing the breadth first search implementation
47+
if __name__ == "__main__":
48+
49+
# Testing on this tree
50+
# 1
51+
# / \
52+
# / \
53+
# 2 3
54+
# / \ / \
55+
# 4 5 6 7
56+
57+
g = Graph()
58+
g.addEdge(1, 2)
59+
g.addEdge(1, 3)
60+
g.addEdge(2, 4)
61+
g.addEdge(2, 5)
62+
g.addEdge(3, 6)
63+
g.addEdge(3, 7)
64+
65+
print("Test 1:")
66+
bfs(1, g)
67+
68+
print("\nTest2:")
69+
bfs(2, g)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#DFS by Master-Fury
2+
import collections
3+
4+
class Graph:
5+
def __init__(self):
6+
self.adjList = collections.defaultdict(set)
7+
8+
def addEdge(self, node1, node2):
9+
self.adjList[node1].add(node2)
10+
self.adjList[node2].add(node1)
11+
12+
def dfsHelper(current, graph, visited, visitFunc):
13+
if (visited[current]):
14+
return
15+
16+
visited[current] = True;
17+
18+
visitFunc(current)
19+
20+
for neighbor in graph.adjList[current]:
21+
dfsHelper(neighbor, graph, visited, visitFunc)
22+
23+
24+
def dfs(current, graph, visitFunc):
25+
visited = collections.defaultdict(bool)
26+
dfsHelper(current, graph, visited, visitFunc)
27+
28+
def visitPrint(i):
29+
print(i)
30+
31+
# Testing the depth first search implementation
32+
if __name__ == "__main__":
33+
34+
# Testing on this tree
35+
# 1
36+
# / \
37+
# / \
38+
# 2 3
39+
# / \ / \
40+
# 4 5 6 7
41+
42+
g = Graph()
43+
g.addEdge(1, 2)
44+
g.addEdge(1, 3)
45+
g.addEdge(2, 4)
46+
g.addEdge(2, 5)
47+
g.addEdge(3, 6)
48+
g.addEdge(3, 7)
49+
50+
print("Test 1:")
51+
dfs(1, g, visitPrint)
52+
53+
print("\nTest2:")
54+
dfs(2, g, visitPrint)

0 commit comments

Comments
 (0)