1 / 13

I nitialize

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.

barney
Download Presentation

I nitialize

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. SIMPLIFICATION (Dijkstra)

  5. 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

  6. 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

  7. 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

  8. SIMPLIFICATION (PRIM)

  9. 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

  10. 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

  11. 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

  12. BFS utilizing dist[], parent[] fields dist[] keeps track of the level parent[] keep track of how vertex was reached

  13. 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

More Related