Shortest Path:
Longest path:
We initialize distances to all vertices as minus infinite and distance to source as 0, then we find topological sorting of the graph. Topological Sorting of a graph represents a linear ordering of the graph (See below, figure (b) is a linear representation of figure (a) ). Once we have topological order (or linear representation), we one by one process all vertices in topological order. For every vertex being processed, we update distances of its adjacent using distance of current vertex.
Reference :: GeeksforGeeks
Java implementation:
For a general weighted graph, we can calculate single source shortest distances in O(VE) time using Bellman–Ford Algorithm. For a graph with no negative weights, we can do better and calculate single source shortest distances in O(E + VLogV) time using Dijkstra’s algorithm. Can we do even better for Directed Acyclic Graph (DAG)? We can calculate single source shortest distances in O(V+E) time for DAGs. The idea is to use Topological Sorting.
We initialize distances to all vertices as infinite and distance to source as 0, then we find a topological sorting of the graph. Topological Sorting of a graph represents a linear ordering of the graph (See below, figure (b) is a linear representation of figure (a) ). Once we have topological order (or linear representation), we one by one process all vertices in topological order. For every vertex being processed, we update distances of its adjacent using distance of current vertex.
Longest path:
We initialize distances to all vertices as minus infinite and distance to source as 0, then we find topological sorting of the graph. Topological Sorting of a graph represents a linear ordering of the graph (See below, figure (b) is a linear representation of figure (a) ). Once we have topological order (or linear representation), we one by one process all vertices in topological order. For every vertex being processed, we update distances of its adjacent using distance of current vertex.
Reference :: GeeksforGeeks
Java implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | package g4g.Graph;/** * Created by seesunda on 11/15/2014. */ import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.*; import java.io.InputStream; class AdjNode { int v; int weight; AdjNode(int v,int w) { this.v = v; this.weight = w; } } class Pathgraph { int v; ArrayList<AdjNode>[] alist; int[] indegree; Pathgraph(int v) { this.v = v; this.alist = new ArrayList[v]; this.indegree = new int[v]; } public void initilaize(){ for(int i=0;i<alist.length;i++) alist[i] = new ArrayList<AdjNode>(); } public void addEdge(int u,int v,int weight) { AdjNode node = new AdjNode(v,weight); alist[u].add(node); indegree[v]++; } public ArrayList<Integer> TopoSort() { Queue<Integer> q = new LinkedList<Integer>(); ArrayList<Integer> result = new ArrayList<Integer>(); boolean[] visited = new boolean[alist.length]; for(int i=0;i<indegree.length;i++) { if(indegree[i] == 0) { q.add(i); visited[i] = true; break;} } while(!q.isEmpty()) { int u = q.poll(); result.add(u); for(int i=0;i<alist[u].size();i++) { AdjNode n = alist[u].get(i); int v = n.v; indegree[v]--; } for(int i=0;i<alist.length;i++) { if(!visited[i] && indegree[i] == 0) { q.add(i); visited[i] = true;} } } return result; } public int[] longestPath(int s) { int[] dist = new int[alist.length]; Arrays.fill(dist,Integer.MIN_VALUE); dist[s] = 0; ArrayList<Integer> topoOrder = TopoSort(); for(int i=s;i<topoOrder.size();i++) { int u = topoOrder.get(i); //System.out.println(u); for(int j=0;j<alist[u].size();j++) { AdjNode node = alist[u].get(j); int v = node.v; int weight = node.weight; //System.out.println(v + " " + weight); if(dist[v] < dist[u] + weight ) dist[v] = dist[u]+weight; } } return dist; } public int[] shortestPath(int s) { int[] dist = new int[alist.length]; Arrays.fill(dist,Integer.MAX_VALUE); dist[s] = 0; ArrayList<Integer> topoOrder = TopoSort(); for(int i=s;i<topoOrder.size();i++) { int u = topoOrder.get(i); //System.out.println(u); for(int j=0;j<alist[u].size();j++) { AdjNode node = alist[u].get(j); int v = node.v; int weight = node.weight; //System.out.println(v + " " + weight); if(dist[v] > dist[u] + weight ) dist[v] = dist[u]+weight; } } return dist; } public static void main(String[] args) { Pathgraph g = new Pathgraph(6); g.initilaize(); g.addEdge(0, 1, 5); g.addEdge(0, 2, 3); g.addEdge(1, 3, 6); g.addEdge(1, 2, 2); g.addEdge(2, 4, 4); g.addEdge(2, 5, 2); g.addEdge(2, 3, 7); g.addEdge(3, 5, 1); g.addEdge(3, 4, -1); g.addEdge(4, 5, -2); int s = 1; System.out.println("Longest distances from source vertex " ); int[] lPath = g.longestPath(s); for(int k : lPath) System.out.print(k + " "); System.out.println(); Pathgraph g1 = new Pathgraph(6); g1.initilaize(); g1.addEdge(0, 1, 5); g1.addEdge(0, 2, 3); g1.addEdge(1, 3, 6); g1.addEdge(1, 2, 2); g1.addEdge(2, 4, 4); g1.addEdge(2, 5, 2); g1.addEdge(2, 3, 7); g1.addEdge(3, 4, -1); g1.addEdge(4, 5, -2); System.out.println("Shortest distances from given source vertex " ); int[] sPath = g1.shortestPath(s); for(int k : sPath) System.out.print(k + " "); } }
|
No comments:
Post a Comment