Skip to content

Commit 5a87ff1

Browse files
authored
Graph implementation (#46)
* Added all the test files into one test_cases folder * Created a folder named Graph_implementation and added 3 files in it, 1)Graph.py which contains all the methods, 2) SearchAlgorithms.py which contains some searching algos. and 3)main.py
1 parent dcb9762 commit 5a87ff1

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

Graph_implementation/Graph.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Graph:
2+
def __init__(self):
3+
self.graph = {}
4+
5+
def add_edge(self, u, v):
6+
if u not in self.graph:
7+
self.graph[u] = []
8+
self.graph[u].append(v)
9+
10+
def add_vertex(self, vertex):
11+
if vertex not in self.graph:
12+
self.graph[vertex] = []
13+
14+
def get_vertices(self):
15+
return list(self.graph.keys())
16+
17+
def get_edges(self):
18+
edges = []
19+
for vertex, neighbors in self.graph.items():
20+
for neighbor in neighbors:
21+
edges.append((vertex, neighbor))
22+
return edges
23+
24+
def is_vertex_exist(self, vertex):
25+
return vertex in self.graph
26+
27+
def is_edge_exist(self, u, v):
28+
return u in self.graph and v in self.graph[u]
29+
30+
def remove_edge(self, u, v):
31+
if self.is_edge_exist(u, v):
32+
self.graph[u].remove(v)
33+
34+
def remove_vertex(self, vertex):
35+
if self.is_vertex_exist(vertex):
36+
del self.graph[vertex]
37+
for u in self.graph:
38+
self.graph[u] = [v for v in self.graph[u] if v != vertex]
39+
40+
def clear(self):
41+
self.graph = {}
42+
43+
def __str__(self):
44+
return str(self.graph)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from collections import deque
2+
3+
def bfs(graph, start):
4+
visited = set()
5+
queue = deque([start])
6+
result = []
7+
8+
while queue:
9+
node = queue.popleft()
10+
if node not in visited:
11+
visited.add(node)
12+
result.append(node)
13+
queue.extend(graph.get(node, []) - visited)
14+
15+
return result
16+
17+
def dfs(graph, start):
18+
visited = set()
19+
stack = [start]
20+
result = []
21+
22+
while stack:
23+
node = stack.pop()
24+
if node not in visited:
25+
visited.add(node)
26+
result.append(node)
27+
stack.extend(graph.get(node, []) - visited)
28+
29+
return result
30+
31+
import heapq
32+
33+
def dijkstra(graph, start):
34+
distances = {vertex: float('infinity') for vertex in graph}
35+
distances[start] = 0
36+
priority_queue = [(0, start)]
37+
38+
while priority_queue:
39+
current_distance, current_vertex = heapq.heappop(priority_queue)
40+
41+
if current_distance > distances[current_vertex]:
42+
continue
43+
44+
for neighbor, weight in graph[current_vertex].items():
45+
distance = current_distance + weight
46+
47+
if distance < distances[neighbor]:
48+
distances[neighbor] = distance
49+
heapq.heappush(priority_queue, (distance, neighbor))
50+
51+
return distances
52+
53+
54+
55+
def a_star(graph, start, goal):
56+
open_set = [(0, start)]
57+
came_from = {}
58+
g_score = {vertex: float('infinity') for vertex in graph}
59+
g_score[start] = 0
60+
f_score = {vertex: float('infinity') for vertex in graph}
61+
f_score[start] = heuristic(start, goal)
62+
63+
while open_set:
64+
_, current = heapq.heappop(open_set)
65+
66+
if current == goal:
67+
return reconstruct_path(came_from, current)
68+
69+
for neighbor, cost in graph[current].items():
70+
tentative_g_score = g_score[current] + cost
71+
72+
if tentative_g_score < g_score[neighbor]:
73+
came_from[neighbor] = current
74+
g_score[neighbor] = tentative_g_score
75+
f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal)
76+
heapq.heappush(open_set, (f_score[neighbor], neighbor))
77+
78+
return None
79+
80+
def heuristic(node, goal):
81+
# Define your heuristic function here (e.g., Manhattan distance, Euclidean distance, etc.)
82+
pass
83+
84+
def reconstruct_path(came_from, current):
85+
path = [current]
86+
while current in came_from:
87+
current = came_from[current]
88+
path.append(current)
89+
return path[::-1]
90+

Graph_implementation/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from Graph_implementation.Graph import Graph
2+
from Graph_implementation.SearchAlgorithms import bfs, dfs, dijkstra, a_star
3+
4+
# Create a graph and add edges
5+
g = Graph()
6+
g.add_edge('A', 'B')
7+
g.add_edge('A', 'C')
8+
g.add_edge('B', 'D')
9+
g.add_edge('B', 'E')
10+
g.add_edge('C', 'F')
11+
12+
# Test BFS and DFS
13+
print("BFS:", bfs(g.graph, 'A'))
14+
print("DFS:", dfs(g.graph, 'A'))

0 commit comments

Comments
 (0)