diff --git a/Graph_implementation/Graph.py b/Graph_implementation/Graph.py new file mode 100644 index 0000000..4919162 --- /dev/null +++ b/Graph_implementation/Graph.py @@ -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) diff --git a/Graph_implementation/SearchAlgorithms.py b/Graph_implementation/SearchAlgorithms.py new file mode 100644 index 0000000..e25f82c --- /dev/null +++ b/Graph_implementation/SearchAlgorithms.py @@ -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] + diff --git a/Graph_implementation/main.py b/Graph_implementation/main.py new file mode 100644 index 0000000..5620aa5 --- /dev/null +++ b/Graph_implementation/main.py @@ -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')) diff --git a/easyPythonpi/TestBasicMath.py b/easyPythonpi/test_cases/TestBasicMath.py similarity index 100% rename from easyPythonpi/TestBasicMath.py rename to easyPythonpi/test_cases/TestBasicMath.py diff --git a/easyPythonpi/TestBin2Hex.py b/easyPythonpi/test_cases/TestBin2Hex.py similarity index 100% rename from easyPythonpi/TestBin2Hex.py rename to easyPythonpi/test_cases/TestBin2Hex.py diff --git a/easyPythonpi/TestFibRefactored.py b/easyPythonpi/test_cases/TestFibRefactored.py similarity index 100% rename from easyPythonpi/TestFibRefactored.py rename to easyPythonpi/test_cases/TestFibRefactored.py diff --git a/test_cases/graph.py Test Cases.py b/test_cases/graph.py Test Cases.py new file mode 100644 index 0000000..c0cd289 --- /dev/null +++ b/test_cases/graph.py Test Cases.py @@ -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() diff --git a/test_cases/main.py Test Cases.py b/test_cases/main.py Test Cases.py new file mode 100644 index 0000000..66a27b2 --- /dev/null +++ b/test_cases/main.py Test Cases.py @@ -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() diff --git a/test_cases/searchalgorithms.py Test Cases.py b/test_cases/searchalgorithms.py Test Cases.py new file mode 100644 index 0000000..506b6a8 --- /dev/null +++ b/test_cases/searchalgorithms.py Test Cases.py @@ -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()