Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Graph_implementation/Graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Graph:
def __init__(self):
self.graph = {}

def add_edge(self, u, v):
if u not in self.graph:
self.graph[u] = []
self.graph[u].append(v)

def add_vertex(self, vertex):
if vertex not in self.graph:
self.graph[vertex] = []

def get_vertices(self):
return list(self.graph.keys())

def get_edges(self):
edges = []
for vertex, neighbors in self.graph.items():
for neighbor in neighbors:
edges.append((vertex, neighbor))
return edges

def is_vertex_exist(self, vertex):
return vertex in self.graph

def is_edge_exist(self, u, v):
return u in self.graph and v in self.graph[u]

def remove_edge(self, u, v):
if self.is_edge_exist(u, v):
self.graph[u].remove(v)

def remove_vertex(self, vertex):
if self.is_vertex_exist(vertex):
del self.graph[vertex]
for u in self.graph:
self.graph[u] = [v for v in self.graph[u] if v != vertex]

def clear(self):
self.graph = {}

def __str__(self):
return str(self.graph)
90 changes: 90 additions & 0 deletions Graph_implementation/SearchAlgorithms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from collections import deque

def bfs(graph, start):
visited = set()
queue = deque([start])
result = []

while queue:
node = queue.popleft()
if node not in visited:
visited.add(node)
result.append(node)
queue.extend(graph.get(node, []) - visited)

return result

def dfs(graph, start):
visited = set()
stack = [start]
result = []

while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
result.append(node)
stack.extend(graph.get(node, []) - visited)

return result

import heapq

def dijkstra(graph, start):
distances = {vertex: float('infinity') for vertex in graph}
distances[start] = 0
priority_queue = [(0, start)]

while priority_queue:
current_distance, current_vertex = heapq.heappop(priority_queue)

if current_distance > distances[current_vertex]:
continue

for neighbor, weight in graph[current_vertex].items():
distance = current_distance + weight

if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(priority_queue, (distance, neighbor))

return distances



def a_star(graph, start, goal):
open_set = [(0, start)]
came_from = {}
g_score = {vertex: float('infinity') for vertex in graph}
g_score[start] = 0
f_score = {vertex: float('infinity') for vertex in graph}
f_score[start] = heuristic(start, goal)

while open_set:
_, current = heapq.heappop(open_set)

if current == goal:
return reconstruct_path(came_from, current)

for neighbor, cost in graph[current].items():
tentative_g_score = g_score[current] + cost

if tentative_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = tentative_g_score
f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal)
heapq.heappush(open_set, (f_score[neighbor], neighbor))

return None

def heuristic(node, goal):
# Define your heuristic function here (e.g., Manhattan distance, Euclidean distance, etc.)
pass

def reconstruct_path(came_from, current):
path = [current]
while current in came_from:
current = came_from[current]
path.append(current)
return path[::-1]

14 changes: 14 additions & 0 deletions Graph_implementation/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from Graph_implementation.Graph import Graph
from Graph_implementation.SearchAlgorithms import bfs, dfs, dijkstra, a_star

# Create a graph and add edges
g = Graph()
g.add_edge('A', 'B')
g.add_edge('A', 'C')
g.add_edge('B', 'D')
g.add_edge('B', 'E')
g.add_edge('C', 'F')

# Test BFS and DFS
print("BFS:", bfs(g.graph, 'A'))
print("DFS:", dfs(g.graph, 'A'))
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions test_cases/graph.py Test Cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Import the necessary modules and classes from graph.py
from easyPythonpi.Graph_implementation.Graph import Graph

# Create an instance of the Graph class
graph = Graph()

# Test adding edges
def test_add_edge():
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'C')
assert 'A' in graph.graph
assert 'B' in graph.graph
assert 'C' in graph.graph
assert 'B' in graph.graph['A']
assert 'C' in graph.graph['A']
assert 'C' in graph.graph['B']

# Run the test
test_add_edge()
25 changes: 25 additions & 0 deletions test_cases/main.py Test Cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Import the necessary modules and classes from main.py
from easyPythonpi.Graph_implementation.main import Graph, bfs, dfs

# Create an instance of the Graph class
graph = Graph()

# Add edges to the graph
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'C')
graph.add_edge('C', 'D')

# Test Breadth-First Search (BFS) from main.py
def test_bfs():
result = bfs(graph.graph, 'A')
assert result == ['A', 'B', 'C', 'D']

# Test Depth-First Search (DFS) from main.py
def test_dfs():
result = dfs(graph.graph, 'A')
assert result == ['A', 'B', 'C', 'D']

# Run the tests
test_bfs()
test_dfs()
26 changes: 26 additions & 0 deletions test_cases/searchalgorithms.py Test Cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Import the necessary modules and functions from searchalgorithms.py
from easyPythonpi.Graph_implementation.SearchAlgorithms import bfs, dfs

# Create a sample graph for testing
graph = {
'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C'],
}

# Test Breadth-First Search (BFS)
def test_bfs():
result = bfs(graph, 'A')
assert result == ['A', 'B', 'C', 'D']

# Test Depth-First Search (DFS)
def test_dfs():
result = dfs(graph, 'A')
assert result == ['A', 'B', 'C', 'D']

# Run the tests
test_bfs()
test_dfs()