20 likes | 129 Views
// Global arrays indexed by vertex number int[] dist // dist[u] = distance from s to u int[] prev // prev[u] = u’s predecessor in BFS tree // Dijkstra’s alg. for G = (V,E), starting at vertex s. void dijkstra (G, s) for each u in V dist[u] = prev[u] = null
E N D
// Global arrays indexed by vertex number int[] dist // dist[u] = distance from s to u int[] prev // prev[u] = u’s predecessor in BFS tree // Dijkstra’s alg. for G = (V,E), starting at vertex s. void dijkstra (G, s) for each u in V dist[u] = prev[u] = null dist[s] = 0 Q = new PriorityQueue(|V|) Q.decreaseKey(s, 0) while (!Q.isEmpty()) u = Q.deleteMin() for each edge (u,v,w) in E if (dist[v] > dist[u] + w) dist[v] = dist[u] + w prev[v] = u Q.decreaseKey(v, dist[v])
// Global arrays indexed by vertex number int[] dist // dist[u] = distance from s to u int[] prev // prev[u] = u’s predecessor in SP tree // BFS of G = (V,E), starting at vertex s. void bfs (G, s) for each u in V dist[u] = prev[u] = null dist[s] = 0 Q = new Queue() Q.enqueue(s) while (!Q.isEmpty()) u = Q.dequeue() for each edge (u,v) in E if (dist[v] = ) dist[v] = dist[u] + 1 prev[v] = u Q.enqueue(v)