-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some Graph Problems Added #6
Changes from all commits
00d5ac2
0204ef5
80f9070
e294765
0a49f20
9bc51ec
d93d550
e83e3c6
d4d72bb
86b8e13
52b43fc
d0c4797
ff60a3b
2db196a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Edge> 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<Edge> graph[]) { | ||
for (int i = 0; i < graph.length; i++) { | ||
graph[i] = new ArrayList<Edge>(); | ||
} | ||
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<Edge> graph[], int V) { | ||
Queue<Integer> 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); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import java.util.ArrayList; | ||
|
||
public class BellmanFord { | ||
Check warning on line 3 in Solved-Problems/GraphTheory/BellmanFord.java GitHub Actions / java-linter / java-linter
|
||
public static void main(String[] args) { | ||
Check warning on line 4 in Solved-Problems/GraphTheory/BellmanFord.java GitHub Actions / java-linter / java-linter
Check warning on line 4 in Solved-Problems/GraphTheory/BellmanFord.java GitHub Actions / java-linter / java-linter
|
||
int vertex = 5; | ||
Check warning on line 5 in Solved-Problems/GraphTheory/BellmanFord.java GitHub Actions / java-linter / java-linter
|
||
@SuppressWarnings("unchecked") | ||
ArrayList<Edge>[] 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<Edge> 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<Edge> 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] + " "); | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import java.util.ArrayList; | ||
|
||
public class DFS { | ||
Check warning on line 3 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
Check warning on line 3 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
// 1- --3 | ||
// / | \ | ||
// 0 | 5--6 | ||
// \ | / | ||
// 2-- - 4 | ||
|
||
public static void main(String[] args) { | ||
Check warning on line 10 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
int vertex = 7; | ||
@SuppressWarnings("unchecked") ArrayList<Edge> [] 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<Edge> [] graph) { | ||
Check warning on line 31 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
Check warning on line 31 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
for (int i = 0; i < graph.length; i++) { | ||
graph[i] = new ArrayList<Edge>(); | ||
} | ||
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<Edge> [] graph, int curr, boolean [] vis) { | ||
Check warning on line 59 in Solved-Problems/GraphTheory/DFS.java GitHub Actions / java-linter / java-linter
|
||
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); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Edge> 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<Edge> graph[]) { | ||
for (int i = 0; i < graph.length; i++) { | ||
graph[i] = new ArrayList<Edge>(); | ||
} | ||
|
||
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<Pair> { | ||
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<Edge> graph[], int src, int V) { | ||
PriorityQueue<Pair> 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] + " "); | ||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the file that's causing the linting to fail, please format the code with provided sources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was no need to remove you source code @DBasu2610, you could have formatted it. The linting checks failed due to indentation and an missing curly brace. And I directed right towards the answer i.e., reviewing diffs. It has the formatted code.
I know the linting workflow might annoy you but they're the ones that help you write clean code following standards. Even after hint the direct solution, you proceed to remove the file and I'm saying it was unnecessary. You could have copy-pasted the solution, removing things won't make it any better.
The solution was here along... You could have asked me even for clarifications, but yet you proceeded on your own...
I gave you the solution and it's up to you whether to use it or not...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I added it back and also formatted it, but it still is having linting issues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you source code is getting reformatted after pasting the formatted code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look here, there are the things that need to changes...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try directly copying from the diffs and pasting them right in the GitHub. That should work...