Skip to content

Commit

Permalink
Added Vertex with Adjacent Vertices,Added Edge List with Respective W…
Browse files Browse the repository at this point in the history
…eights And Finally implemented Dijkstra's Algorithm
  • Loading branch information
eklaDFF committed Jan 10, 2024
1 parent 1529560 commit 6dc3210
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Graph.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
24 changes: 24 additions & 0 deletions src/EdgeArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.ArrayList;
public class EdgeArrayList {
private class EdgeArrayListNode {
Vertex a,b;
int weight;
public EdgeArrayListNode(Vertex a,Vertex b,int weight){
this.a = a;
this.b = b;
this.weight = weight;
}
}
ArrayList<EdgeArrayListNode> edgeArrayList = new ArrayList<>();
public void addEdge(Vertex s,Vertex d,int w){
edgeArrayList.add(new EdgeArrayListNode(s,d,w));
}
public int getWeight(Vertex s,Vertex d){
for (EdgeArrayListNode node : edgeArrayList){
if ((node.a.name == s.name) && (node.b.name==d.name)){
return node.weight;
}
}
return -1;
}
}
55 changes: 54 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
import java.util.Comparator;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Vertex A = new Vertex('A');
Vertex B = new Vertex('B');
Vertex C = new Vertex('C');
Vertex D = new Vertex('D');
Vertex E = new Vertex('E');

A.adjacentVertices.add(B);
A.adjacentVertices.add(C);
B.adjacentVertices.add(E);
C.adjacentVertices.add(B);
C.adjacentVertices.add(D);
D.adjacentVertices.add(E);

EdgeArrayList edgeArrayList = new EdgeArrayList();
edgeArrayList.addEdge(A,B,4);
edgeArrayList.addEdge(A,C,1);
edgeArrayList.addEdge(B,E,4);
edgeArrayList.addEdge(C,B,2);
edgeArrayList.addEdge(C,D,4);
edgeArrayList.addEdge(D,E,4);

DijkstraAlgo(A,E,edgeArrayList);
}
public static void DijkstraAlgo(Vertex source,Vertex destination,EdgeArrayList edgeArrayList){
PriorityQueue<Vertex> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(o -> o.pathDistanceFromSourceVertex));
source.pathAdjacentSourceVertex = source;
source.pathDistanceFromSourceVertex = 0;
priorityQueue.add(source);
while(!priorityQueue.isEmpty()){
Vertex c = priorityQueue.remove();
if (c.explored){continue;}
for (Vertex adj : c.adjacentVertices){
if (adj.explored){continue;}
if (adj.pathDistanceFromSourceVertex>(c.pathDistanceFromSourceVertex + edgeArrayList.getWeight(c,adj))){
adj.pathDistanceFromSourceVertex = c.pathDistanceFromSourceVertex + edgeArrayList.getWeight(c,adj);
adj.pathAdjacentSourceVertex = c;
}
priorityQueue.add(adj);
}
c.explored = true;
}

// Printing the Shortest Path
Vertex c = destination;
StringBuilder path = new StringBuilder(c.name + "(Cost to reach here " + destination.pathDistanceFromSourceVertex +") <- ");
c = c.pathAdjacentSourceVertex;
while (c.name != source.name){
path.append(c.name).append(" <- ");
c = c.pathAdjacentSourceVertex;
}
path.append(c.name);
System.out.println(path);
}
}
15 changes: 15 additions & 0 deletions src/Vertex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.ArrayList;
public class Vertex {
char name;
boolean explored;
int pathDistanceFromSourceVertex;
ArrayList<Vertex> adjacentVertices;
Vertex pathAdjacentSourceVertex;
public Vertex (char name){
this.name = name;
explored = false;
pathDistanceFromSourceVertex = Integer.MAX_VALUE;
adjacentVertices = new ArrayList<>();
pathAdjacentSourceVertex = null;
}
}

0 comments on commit 6dc3210

Please sign in to comment.