Skip to content
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

Closed
wants to merge 14 commits into from
74 changes: 74 additions & 0 deletions Solved-Problems/GraphTheory/BFS.java
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);
}
}

}
}
}
72 changes: 72 additions & 0 deletions Solved-Problems/GraphTheory/BellmanFord.java
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

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/BellmanFord.java:3:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)
public static void main(String[] args) {

Check warning on line 4 in Solved-Problems/GraphTheory/BellmanFord.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'method def modifier' has incorrect indentation level 4, expected level should be 2. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/BellmanFord.java:4:5: warning: 'method def modifier' has incorrect indentation level 4, expected level should be 2. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)

Check warning on line 4 in Solved-Problems/GraphTheory/BellmanFord.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/BellmanFord.java:4:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
int vertex = 5;

Check warning on line 5 in Solved-Problems/GraphTheory/BellmanFord.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'method def' child has incorrect indentation level 8, expected level should be 4. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/BellmanFord.java:5:9: warning: 'method def' child has incorrect indentation level 8, expected level should be 4. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
@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] + " ");

}
}
}
69 changes: 69 additions & 0 deletions Solved-Problems/GraphTheory/DFS.java
Copy link
Contributor

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.

Copy link
Contributor

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...
image

I gave you the solution and it's up to you whether to use it or not...

Copy link
Author

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

Copy link
Contributor

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.

Copy link
Contributor

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...

image
image
image

Copy link
Contributor

@iamwatchdogs iamwatchdogs Sep 30, 2024

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...

like this,
image

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

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/DFS.java:3:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)

Check warning on line 3 in Solved-Problems/GraphTheory/DFS.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 Abbreviation in name 'DFS' must contain no more than '1' consecutive capital letters. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/DFS.java:3:14: warning: Abbreviation in name 'DFS' must contain no more than '1' consecutive capital letters. (com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck)
// 1- --3
// / | \
// 0 | 5--6
// \ | /
// 2-- - 4

public static void main(String[] args) {

Check warning on line 10 in Solved-Problems/GraphTheory/DFS.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/DFS.java:10:3: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
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

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'method def modifier' has incorrect indentation level 3, expected level should be 2. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/DFS.java:31:4: warning: 'method def modifier' has incorrect indentation level 3, expected level should be 2. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)

Check warning on line 31 in Solved-Problems/GraphTheory/DFS.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/DFS.java:31:4: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
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

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./Solved-Problems/GraphTheory/DFS.java:59:3: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
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);
}
}
}
}
100 changes: 100 additions & 0 deletions Solved-Problems/GraphTheory/Dijkstra.java
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] + " ");
}

}

}
18 changes: 18 additions & 0 deletions Solved-Problems/GraphTheory/README.md
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

Loading