diff --git a/Solved-Problems/GraphTheory/BFS.java b/Solved-Problems/GraphTheory/BFS.java new file mode 100644 index 0000000..47ce160 --- /dev/null +++ b/Solved-Problems/GraphTheory/BFS.java @@ -0,0 +1,74 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; + +public class BFS { + + // 1- --3 + // / | \ + // 0 | 5--6 + // \ | / + // 2-- - 4 + public static void main(String[] args) { + int vertex = 7; + @SuppressWarnings("unchecked") + ArrayList graph[] = new ArrayList[vertex]; + + createGraph(graph); + + breadthFirst(graph, vertex); + System.out.println(); + } + + static class Edge { + int src; + int dest; + + public Edge(int s, int d) { + this.src = s; + this.dest = d; + } + } + + public static void createGraph(ArrayList graph[]) { + for (int i = 0; i < graph.length; i++) { + graph[i] = new ArrayList(); + } + graph[0].add(new Edge(0, 1)); + graph[0].add(new Edge(0, 2)); + graph[1].add(new Edge(1, 0)); + graph[1].add(new Edge(1, 3)); + graph[2].add(new Edge(2, 4)); + graph[2].add(new Edge(2, 0)); + graph[3].add(new Edge(3, 1)); + graph[3].add(new Edge(3, 5)); + graph[3].add(new Edge(3, 4)); + graph[4].add(new Edge(4, 5)); + graph[4].add(new Edge(4, 3)); + graph[4].add(new Edge(4, 2)); + graph[5].add(new Edge(5, 6)); + graph[5].add(new Edge(5, 3)); + graph[5].add(new Edge(5, 4)); + graph[6].add(new Edge(6, 5)); + + } + + public static void breadthFirst(ArrayList graph[], int V) { + Queue q = new LinkedList<>(); + boolean vis[] = new boolean[V]; + q.add(0); + + while (!q.isEmpty()) { + int curr = q.remove(); + if (vis[curr] == false) { + System.out.println(curr); + vis[curr] = true; + for (int i = 0; i < graph[curr].size(); i++) { + Edge e = graph[curr].get(i); + q.add(e.dest); + } + } + + } + } +} diff --git a/Solved-Problems/GraphTheory/BellmanFord.java b/Solved-Problems/GraphTheory/BellmanFord.java new file mode 100644 index 0000000..b1d6adb --- /dev/null +++ b/Solved-Problems/GraphTheory/BellmanFord.java @@ -0,0 +1,72 @@ +import java.util.ArrayList; + +public class BellmanFord { + public static void main(String[] args) { + int vertex = 5; + @SuppressWarnings("unchecked") + ArrayList[] graph = new ArrayList[vertex]; + + create(graph); + bf(graph, 0, vertex); + + } + + static class Edge { + int src; + int dest; + int weight; + + public Edge(int s, int d, int w) { + this.src = s; + this.dest = d; + this.weight = w; + } + } + + public static void create(ArrayList graph[]) { + for (int i = 0; i < graph.length; i++) { + graph[i] = new ArrayList<>(); + } + graph[0].add(new Edge(0, 1, 2)); + graph[0].add(new Edge(0, 2, 4)); + + graph[1].add(new Edge(1, 2, -4)); + + graph[2].add(new Edge(2, 3, 2)); + + graph[3].add(new Edge(3, 4, 4)); + + graph[4].add(new Edge(4, 1, -1)); + + } + + public static void bf(ArrayList graph[], int src, int V) { + int[] dis = new int[V]; + for (int i = 0; i < V; i++) { + if (i != src) { + dis[i] = Integer.MAX_VALUE; + } + } + + for (int k = 0; k < V - 1; k++) { + for (int j = 0; j < V; j++) { + for (int i = 0; i < graph[i].size(); i++) { + Edge e = graph[j].get(i); + int u = e.src; + int v = e.dest; + + if (dis[u] < Integer.MAX_VALUE && dis[u] + e.weight < dis[v]) { + dis[v] = dis[u] + e.weight; + } + + } + } + + } + + for (int i = 0; i < dis.length; i++) { + System.err.println(dis[i] + " "); + + } + } +} diff --git a/Solved-Problems/GraphTheory/DFS.java b/Solved-Problems/GraphTheory/DFS.java new file mode 100644 index 0000000..743478c --- /dev/null +++ b/Solved-Problems/GraphTheory/DFS.java @@ -0,0 +1,69 @@ +import java.util.ArrayList; + +public class DFS { + // 1- --3 + // / | \ + // 0 | 5--6 + // \ | / + // 2-- - 4 + + public static void main(String[] args) { + int vertex = 7; + @SuppressWarnings("unchecked") ArrayList [] graph = new ArrayList[vertex]; + boolean [] vis = new boolean[vertex]; + + createGraph(graph); + + depthFirst(graph, 0, vis); + System.out.println(); + } + + static class Edge { + int src; + int dest; + + public Edge(int s, int d) { + this.src = s; + this.dest = d; + } + } + + public static void createGraph(ArrayList [] graph) { + for (int i = 0; i < graph.length; i++) { + graph[i] = new ArrayList(); + } + graph[0].add(new Edge(0, 1)); + graph[0].add(new Edge(0, 2)); + + graph[1].add(new Edge(1, 0)); + graph[1].add(new Edge(1, 3)); + + graph[2].add(new Edge(2, 0)); + graph[2].add(new Edge(2, 4)); + + graph[3].add(new Edge(3, 1)); + graph[3].add(new Edge(3, 4)); + graph[3].add(new Edge(3, 5)); + + graph[4].add(new Edge(4, 2)); + graph[4].add(new Edge(4, 3)); + graph[4].add(new Edge(4, 5)); + + graph[5].add(new Edge(5, 3)); + graph[5].add(new Edge(5, 4)); + graph[5].add(new Edge(5, 6)); + + graph[6].add(new Edge(6, 5)); + } + + public static void depthFirst(ArrayList [] graph, int curr, boolean [] vis) { + System.out.println(curr); + vis[curr] = true; + for (int i = 0; i < graph[curr].size(); i++) { + Edge e = graph[curr].get(i); + if (vis[e.dest] == false) { + depthFirst(graph, e.dest, vis); + } + } + } +} diff --git a/Solved-Problems/GraphTheory/Dijkstra.java b/Solved-Problems/GraphTheory/Dijkstra.java new file mode 100644 index 0000000..a6d5f9e --- /dev/null +++ b/Solved-Problems/GraphTheory/Dijkstra.java @@ -0,0 +1,100 @@ + +import java.util.ArrayList; +import java.util.PriorityQueue; + +public class Dijkstra { + public static void main(String[] args) { + int vertex = 6; + @SuppressWarnings("unchecked") + ArrayList graph[] = new ArrayList[vertex]; + create(graph); + + ShortestPath(graph, 0, vertex); + + } + + static class Edge { + int src; + int dest; + int weight; + + public Edge(int s, int d, int w) { + this.src = s; + this.dest = d; + this.weight = w; + } + + } + + public static void create(ArrayList graph[]) { + for (int i = 0; i < graph.length; i++) { + graph[i] = new ArrayList(); + } + + graph[0].add(new Edge(0, 1, 2)); + graph[0].add(new Edge(0, 2, 4)); + + graph[1].add(new Edge(1, 3, 7)); + graph[1].add(new Edge(1, 2, 1)); + + graph[2].add(new Edge(2, 4, 3)); + + graph[3].add(new Edge(3, 5, 1)); + + graph[4].add(new Edge(4, 3, 2)); + graph[4].add(new Edge(4, 5, 5)); + + } + + public static class Pair implements Comparable { + int node; + int distance; + + public Pair(int n, int d) { + this.node = n; + this.distance = d; + } + + @Override + public int compareTo(Pair p2) { + return this.distance - p2.distance; + + } + } + + public static void ShortestPath(ArrayList graph[], int src, int V) { + PriorityQueue pq = new PriorityQueue<>(); + boolean vis[] = new boolean[V]; + int dist[] = new int[V]; + for (int i = 0; i < V; i++) { + if (i != src) { + dist[i] = Integer.MAX_VALUE; + } + } + pq.add(new Pair(0, 0)); + + while (!pq.isEmpty()) { + Pair curr = pq.remove(); + if (!vis[curr.node]) { + vis[curr.node] = true; + for (int i = 0; i < graph[curr.node].size(); i++) { + Edge e = graph[curr.node].get(i); + int u = e.src; + int v = e.dest; + + if (dist[u] + e.weight < dist[v]) { + dist[v] = dist[u] + e.weight; + pq.add(new Pair(v, dist[v])); + } + + } + } + } + + for (int i = 0; i < V; i++) { + System.out.print(dist[i] + " "); + } + + } + +} diff --git a/Solved-Problems/GraphTheory/README.md b/Solved-Problems/GraphTheory/README.md new file mode 100644 index 0000000..42a4d2b --- /dev/null +++ b/Solved-Problems/GraphTheory/README.md @@ -0,0 +1,18 @@ + +Problem Name: Breadth-First Search (BFS) +Approach: Graph traversal using Breadth-First Search +Difficulty: Easy/Medium + +Problem Name: Depth-First Search (DFS) +Approach: Graph traversal using Depth-First Search +Difficulty: Medium + +Problem Name: Dijkstra’s Algorithm +Approach: Shortest Path Algorithm using Dijkstra's Algorithm (Greedy approach) +Difficulty: Medium/Hard + + +Problem Name: Bellman-Ford Algorithm +Approach: Single-source shortest path using Bellman-Ford Algorithm (Dynamic Programming) +Difficulty: Medium/Hard +