-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from muzhar01/bfs
Implement Breadth First Search (BFS) Algorithm in Python, C, and Java
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import java.util.*; | ||
|
||
class Graph { | ||
private Map<Integer, List<Integer>> adjList = new HashMap<>(); | ||
|
||
public void addEdge(int u, int v) { | ||
adjList.putIfAbsent(u, new ArrayList<>()); | ||
adjList.get(u).add(v); | ||
} | ||
|
||
public void bfs(int start) { | ||
Set<Integer> visited = new HashSet<>(); | ||
Queue<Integer> queue = new LinkedList<>(); | ||
visited.add(start); | ||
queue.add(start); | ||
|
||
while (!queue.isEmpty()) { | ||
int v = queue.poll(); | ||
System.out.print(v + " "); | ||
for (int neighbor : adjList.getOrDefault(v, new ArrayList<>())) { | ||
if (!visited.contains(neighbor)) { | ||
visited.add(neighbor); | ||
queue.add(neighbor); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Example usage | ||
public static void main(String[] args) { | ||
Graph g = new Graph(); | ||
g.addEdge(1, 2); | ||
g.addEdge(1, 3); | ||
g.addEdge(2, 4); | ||
g.bfs(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Graph Algorithms: Breadth First Search (BFS) | ||
|
||
This project contains implementations of the Breadth First Search (BFS) algorithm in Python, C, and Java. | ||
|
||
## Problem Overview | ||
|
||
Graphs are fundamental data structures used to represent relationships between objects. A graph is composed of vertices (nodes) and edges (connections between nodes). One common operation on graphs is traversing them to explore their structure, which can be accomplished using algorithms like Breadth First Search (BFS). | ||
|
||
### Purpose | ||
|
||
The purpose of this project is to provide a simple implementation of a graph data structure and demonstrate how to perform a Breadth First Search (BFS) traversal on it. | ||
|
||
### Implementations | ||
|
||
- **Python**: The Python implementation uses a dictionary to represent the adjacency list of the graph. The `bfs` method performs a breadth-first traversal, printing each visited vertex. | ||
|
||
- **C**: The C implementation uses a 2D array to represent the graph's edges. The `bfs` function utilizes a queue to traverse the graph iteratively and marks visited vertices using an array. | ||
|
||
- **Java**: The Java implementation uses a HashMap to store the adjacency list of the graph. The `bfs` method utilizes a queue and a set to track visited vertices during traversal. | ||
|
||
### Usage | ||
|
||
To run the examples: | ||
|
||
1. **Python**: Execute the `bfs.py` file using Python 3. | ||
2. **C**: Compile and run the `bfs.c` file using a C compiler. | ||
3. **Java**: Compile and run the `BFS.java` file using the Java Development Kit (JDK). | ||
|
||
### Conclusion | ||
|
||
This project serves as an introduction to graph data structures and demonstrates the implementation of breadth-first search. Contributions are welcome to expand on this foundation with more complex algorithms and features. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define MAX 100 | ||
|
||
typedef struct { | ||
int edges[MAX][MAX]; | ||
int numVertices; | ||
} Graph; | ||
|
||
void initGraph(Graph *g, int vertices) { | ||
g->numVertices = vertices; | ||
for (int i = 0; i < MAX; i++) | ||
for (int j = 0; j < MAX; j++) | ||
g->edges[i][j] = 0; | ||
} | ||
|
||
void addEdge(Graph *g, int u, int v) { | ||
g->edges[u][v] = 1; | ||
} | ||
|
||
void bfs(Graph *g, int start) { | ||
int visited[MAX] = {0}; | ||
int queue[MAX], front = 0, rear = 0; | ||
|
||
visited[start] = 1; | ||
queue[rear++] = start; | ||
|
||
while (front < rear) { | ||
int v = queue[front++]; | ||
printf("%d ", v); | ||
for (int i = 0; i < g->numVertices; i++) { | ||
if (g->edges[v][i] && !visited[i]) { | ||
visited[i] = 1; | ||
queue[rear++] = i; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Example usage | ||
int main() { | ||
Graph g; | ||
initGraph(&g, 5); | ||
addEdge(&g, 0, 1); | ||
addEdge(&g, 0, 2); | ||
addEdge(&g, 1, 3); | ||
|
||
bfs(&g, 0); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from collections import deque | ||
|
||
class Graph: | ||
def __init__(self): | ||
self.graph = {} | ||
|
||
def add_edge(self, u, v): | ||
self.graph.setdefault(u, []).append(v) | ||
|
||
def bfs(self, start): | ||
visited = set() | ||
queue = deque([start]) | ||
visited.add(start) | ||
|
||
while queue: | ||
v = queue.popleft() | ||
print(v, end=' ') | ||
for neighbor in self.graph.get(v, []): | ||
if neighbor not in visited: | ||
visited.add(neighbor) | ||
queue.append(neighbor) | ||
|
||
# Example usage | ||
g = Graph() | ||
g.add_edge(1, 2) | ||
g.add_edge(1, 3) | ||
g.add_edge(2, 4) | ||
g.bfs(1) |