-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Vertex with Adjacent Vertices,Added Edge List with Respective W…
…eights And Finally implemented Dijkstra's Algorithm
- Loading branch information
Showing
8 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |