├── .gitignore ├── LICENSE └── src ├── BreadthFirstSearch.java ├── DepthFirstSearch.java ├── DijkstraShortestPath.java ├── Graph.java ├── KruskalMST.java ├── PrimMST.java └── Vertex.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Keval Morabia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/BreadthFirstSearch.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | 3 | public class BreadthFirstSearch extends Graph { 4 | public BreadthFirstSearch(int v) { 5 | super(v); 6 | } 7 | 8 | void BFS(int start){ 9 | for(Vertex v: vertex){ 10 | v.color=0; 11 | v.distance=-1;//if after BFS, distance=-1 --> unreachable 12 | v.parent=null; 13 | } 14 | LinkedList queue = new LinkedList<>(); 15 | vertex[start].color=1; 16 | vertex[start].distance=0; 17 | queue.add(vertex[start]); 18 | while(!queue.isEmpty()){ 19 | Vertex u = queue.removeFirst(); 20 | for(Vertex v : u.adj){ 21 | if(v.color==0){ 22 | v.color=1; 23 | v.distance=u.distance+1; 24 | v.parent=u; 25 | queue.add(v); 26 | } 27 | } 28 | u.color=2; 29 | } 30 | } 31 | 32 | void printPath(int start, int end){ 33 | System.out.print("Path from "+start+" to "+end+": "); 34 | BFS(start); 35 | print(start, end); 36 | System.out.println(); 37 | } 38 | 39 | void print(int start, int end){ 40 | if(end==start) 41 | System.out.print(start+" "); 42 | else if(vertex[end].parent==null) 43 | System.out.println("No path from "+start+" to "+end+" exists."); 44 | else { 45 | print(start,vertex[end].parent.data); 46 | System.out.print(end+" ") ; 47 | } 48 | } 49 | 50 | public static void main(String[] args){ 51 | BreadthFirstSearch g = new BreadthFirstSearch(5); 52 | g.addEdge(0, 1); 53 | g.addEdge(1, 2); 54 | g.addEdge(2, 0); 55 | g.addEdge(3, 3); 56 | g.addEdge(4, 2); 57 | 58 | g.printPath(1, 4); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/DepthFirstSearch.java: -------------------------------------------------------------------------------- 1 | public class DepthFirstSearch extends Graph{ 2 | int time; 3 | 4 | public DepthFirstSearch(int v) { 5 | super(v); 6 | time=0; 7 | } 8 | 9 | void DFS(){ 10 | time=0; 11 | for(int i=0;i vertex[u].distance+w){ 14 | vertex[v].distance = vertex[u].distance+w; 15 | vertex[v].parent = vertex[u]; 16 | } 17 | } 18 | 19 | void dijkstra(int start){ 20 | initialize(); 21 | vertex[start].extracted=true; 22 | vertex[start].distance = 0; 23 | int u = start; 24 | while(u!=-1){ 25 | for(Vertex ver : vertex[u].adj){ 26 | relax(u,ver.data,weight[u][ver.data]); 27 | } 28 | u = extractMin(); 29 | } 30 | 31 | } 32 | 33 | void initialize(){ 34 | for(int i = 0; i l){ 22 | for(Vertex v: l){ 23 | System.out.print(v.data+" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | void printAdjacencyList(){ 29 | for(Vertex v: vertex){ 30 | System.out.print("Vertex "+v.data+": "); 31 | printList(v.adj); 32 | } 33 | } 34 | 35 | public static void main(String[] args) { 36 | Graph g = new Graph(5); 37 | g.addEdge(0, 1); 38 | g.addEdge(1, 2); 39 | g.addEdge(2, 0); 40 | g.addEdge(3, 3); 41 | g.addEdge(4, 2); 42 | g.printAdjacencyList(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/KruskalMST.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Comparator; 3 | import java.util.Scanner; 4 | 5 | class Checker implements Comparator{ 6 | public int compare(int[] o1, int[] o2) { 7 | return Integer.compare(o1[2], o2[2]); 8 | } 9 | } 10 | 11 | public class KruskalMST extends Graph{ 12 | int cost; 13 | boolean cycle; 14 | 15 | public KruskalMST(int v) { 16 | super(v); 17 | cost = 0; 18 | cycle = false; 19 | } 20 | 21 | void Kruskal(int[][] edges){ 22 | for(int i = 0; i adj; 5 | int data; 6 | int color;//0=white, 1=gray, 2=black 7 | int distance;//from start vertex 8 | Vertex parent; 9 | int discoveryTime; 10 | int finishTime; 11 | boolean extracted; 12 | 13 | public Vertex(int data){ 14 | this.data=data; 15 | color=0; 16 | distance=-1; 17 | parent=null; 18 | adj = new LinkedList<>(); 19 | discoveryTime=0; 20 | finishTime=0; 21 | } 22 | } 23 | --------------------------------------------------------------------------------