130 likes | 239 Views
I nitialize. d ef BFS (G , source): for v in G: color[v] <- WHITE Q <- MAKE-QUEUE () ADD(Q, source) color[source] <- GRAY while Q not empty: v <- REMOVE(Q) color[v] <- BLACK for u adjacent to v: if color [u ] == WHITE color[ u] = GRAY ADD(Q, u). Relax. I nitialize.
E N D
Initialize def BFS(G, source):for v in G: color[v] <- WHITEQ <- MAKE-QUEUE()ADD(Q, source) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:ifcolor[u] == WHITEcolor[u] = GRAYADD(Q, u) Relax
Initialize def DIJKSTRA(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, source) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:if dist[u] > dist[v]+weight(u,v) dist[u] = dist[v]+weight(u,v) parent[u] <- vif color[u] == WHITE: color[u] <- GRAYADD(Q, u) Relax
Initialize def PRIM(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, source) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:if color[u] != BLACK and dist[u] > weight(u,v): dist[u] = weight(u,v) parent[u] <- vif color[u] == WHITE: color[u] <- GRAYADD(Q, u) Relax
SIMPLIFICATION (Dijkstra)
Initialize def DIJKSTRA(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, source) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:if dist[u] > dist[v]+weight(u,v) dist[u] = dist[v]+weight(u,v) parent[u] <- vif color[u] == WHITE: color[u] <- GRAYADD(Q, u) Relax
Initialize def DIJKSTRA(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, source) ADD(Q, all v in G) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q)color[v] <- BLACKfor u adjacent to v:if dist[u] > dist[v]+weight(u,v) dist[u] = dist[v]+weight(u,v) parent[u] <- vif color[u] == WHITE:color[u] <- GRAYADD(Q, u) Relax
Initialize def DIJKSTRA(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, all v in G) while Q not empty:v <- REMOVE(Q)for u adjacent to v:if dist[u] > dist[v]+weight(u,v) dist[u] = dist[v]+weight(u,v) parent[u] <- v Relax
SIMPLIFICATION (PRIM)
Initialize def PRIM(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, source) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:if color[u] != BLACK and dist[u] > weight(u,v): dist[u] = weight(u,v) parent[u] <- vif color[u] == WHITE: color[u] <- GRAYADD(Q, u) Relax
Initialize def PRIM(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, source) ADD(Q, all v in G) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:if color[u] != BLACK and dist[u] > weight(u,v): dist[u] = weight(u,v) parent[u] <- vif color[u] == WHITE:color[u] <- GRAYADD(Q, u) Relax
Initialize def PRIM(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-PRIORITY-QUEUE()ADD(Q, all v in G) while Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:if color[u] != BLACK and dist[u] > weight(u,v): dist[u] = weight(u,v) parent[u] <- v Relax
BFS utilizing dist[], parent[] fields dist[] keeps track of the level parent[] keep track of how vertex was reached
Initialize def DIJKSTRA(G, source):for v in G: color[v] <- WHITE dist[v] <- INF parent[v] <- NIL dist[source] <- 0Q <- MAKE-QUEUE()ADD(Q, source) color[source] <- GRAYwhile Q not empty:v <- REMOVE(Q) color[v] <- BLACKfor u adjacent to v:ifcolor[u] == WHITE: dist[u] = dist[v]+1 parent[u] <- v color[u] <- GRAYADD(Q, u) Relax