Skip to content

Commit f12d9ae

Browse files
committed
Completed Dijkstra's Algorithm
1 parent e57a5da commit f12d9ae

File tree

3 files changed

+127
-29
lines changed

3 files changed

+127
-29
lines changed

Algorithms/Dijkstra/src/ShortestPath.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
@SuppressWarnings("Duplicates")
5+
public class ShortestPathAdjacencyMatrix {
6+
7+
static class MyGraph{
8+
9+
int[][] adjacency_matrix ;
10+
int vertices ;
11+
12+
MyGraph(int vertices){
13+
this.vertices = vertices ;
14+
this.adjacency_matrix = new int[vertices][vertices];
15+
}
16+
17+
public void add_edge(int src, int dest , int weight){
18+
adjacency_matrix[src][dest] = weight ;
19+
adjacency_matrix[dest][src] = weight ;
20+
}
21+
22+
public void display_matrix(){
23+
for (int i = 0 ; i < this.vertices ; i++){
24+
for (int j = 0 ; j < this.vertices ; j++){
25+
System.out.print(this.adjacency_matrix[i][j] + " ");
26+
}
27+
System.out.println();
28+
}
29+
}
30+
}
31+
32+
public static void main(String[] args) {
33+
34+
// Cross Check Graph from https://www.youtube.com/watch?v=WN3Rb9wVYDY
35+
36+
int vertices = 6 ;
37+
38+
MyGraph myGraph = new MyGraph(vertices) ;
39+
40+
myGraph.add_edge(0 , 1 , 4);
41+
myGraph.add_edge(0 , 2 , 2);
42+
myGraph.add_edge(1 , 3 , 5);
43+
myGraph.add_edge(1 , 2 , 1);
44+
myGraph.add_edge(2 , 3 , 8);
45+
myGraph.add_edge(2 , 4 , 10);
46+
myGraph.add_edge(3 , 4 , 2);
47+
myGraph.add_edge(3 , 5 , 6);
48+
myGraph.add_edge(4 , 5 , 3);
49+
50+
System.out.println("Your Adjacency Matrix : ");
51+
52+
myGraph.display_matrix();
53+
54+
55+
int[] dist_arr = new int[vertices];
56+
boolean[] visited = new boolean[vertices];
57+
58+
visited[0] = true ;
59+
60+
for (int i = 0 ; i < vertices ; i++){
61+
if (i != 0){
62+
dist_arr[i] = Integer.MAX_VALUE;
63+
}
64+
}
65+
66+
int start_node = 0 , end_node = 5 , selected_node = start_node;
67+
68+
System.out.print("Shortest Path -> " + selected_node);
69+
70+
while (!visited[end_node]){
71+
int min_node = 0;
72+
int min_weight = Integer.MAX_VALUE ;
73+
for (int i = 0 ; i < vertices ; i++){
74+
int temp_weight = myGraph.adjacency_matrix[selected_node][i] ;
75+
if (temp_weight != 0 && !visited[i]){
76+
if (temp_weight < dist_arr[i]){
77+
dist_arr[i] = temp_weight ;
78+
}
79+
if (temp_weight < min_weight){
80+
min_weight = temp_weight ;
81+
min_node = i ;
82+
}
83+
}
84+
}
85+
selected_node = min_node ;
86+
visited[selected_node] = true ;
87+
System.out.print(" -> " + selected_node);
88+
//System.out.println(Arrays.toString(dist_arr));
89+
//System.out.println(Arrays.toString(visited));
90+
}
91+
}
92+
93+
}

Data Structures/Simple Graph Representation/src/AdjacencyMatrix.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
11
import java.util.Scanner;
22

3+
@SuppressWarnings("Duplicates")
34
public class AdjacencyMatrix {
45

5-
public static void main(String[] args) {
6+
static class MyGraph{
67

7-
Scanner sc = new Scanner(System.in);
8+
int[][] adjacency_matrix ;
9+
int vertices ;
810

9-
System.out.print("Enter number of vertices : ");
11+
MyGraph(int vertices){
12+
this.vertices = vertices ;
13+
this.adjacency_matrix = new int[vertices][vertices];
14+
}
1015

11-
int vertices = sc.nextInt();
16+
public void add_edge(int src, int dest , int weight){
17+
adjacency_matrix[src][dest] = weight ;
18+
adjacency_matrix[dest][src] = weight ;
19+
}
1220

13-
int[][] adjacency_matrix = new int[vertices][vertices];
21+
public void display_matrix(){
22+
for (int i = 0 ; i < this.vertices ; i++){
23+
for (int j = 0 ; j < this.vertices ; j++){
24+
System.out.print(this.adjacency_matrix[i][j] + " ");
25+
}
26+
System.out.println();
27+
}
28+
}
1429

15-
String status = "y" ;
30+
}
1631

17-
while(status.equals("y")){
32+
public static void main(String[] args) {
1833

19-
System.out.print("Enter the edge : ");
20-
int start = sc.nextInt();
21-
int end = sc.nextInt();
34+
int vertices = 6 ;
2235

23-
adjacency_matrix[start-1][end-1] = 1 ;
24-
adjacency_matrix[end-1][start-1] = 1 ;
36+
MyGraph myGraph = new MyGraph(vertices) ;
2537

26-
System.out.print("Do you want to add another edge : ");
27-
status = sc.next();
28-
}
38+
myGraph.add_edge(0 , 1 , 4);
39+
myGraph.add_edge(0 , 2 , 2);
40+
myGraph.add_edge(1 , 3 , 5);
41+
myGraph.add_edge(1 , 2 , 1);
42+
myGraph.add_edge(2 , 3 , 8);
43+
myGraph.add_edge(2 , 4 , 10);
44+
myGraph.add_edge(3 , 4 , 2);
45+
myGraph.add_edge(3 , 5 , 6);
46+
myGraph.add_edge(4 , 5 , 3);
2947

3048
System.out.println("Your Adjacency Matrix : ");
3149

32-
for (int i = 0 ; i < vertices ; i++){
33-
for (int j = 0 ; j < vertices ; j++){
34-
System.out.print(adjacency_matrix[i][j] + " ");
35-
}
36-
System.out.println();
37-
}
50+
myGraph.display_matrix();
3851

3952
}
4053

0 commit comments

Comments
 (0)