![Page 1: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/1.jpg)
CS261 Data Structures
Single Source Reachability - Edge List Representation
![Page 2: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/2.jpg)
Question
• What nodes are reachable from Peoria?
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
![Page 3: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/3.jpg)
Single Source Reachability: Edge-List
findReachable (graph g, vertex start) { create a set of reachable vertices, initially empty. call this r. create a container for vertices known to be reachable. call this c add start vertex to container c while the container c is not empty { remove first entry from the container c, assign to v if v is not already in the set of reachable vertices r { add v to the reachable set r add the neighbors of v, not already reachable, to the container creturn r
![Page 4: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/4.jpg)
Single Source Reachability: Stack
• Let’s use a Stack as our container• Basic algorithm:
Initialize set of reachable vertices and add vi to a stack
While stack is not emptyGet and remove (pop) last vertex v from stack
if vertex v is not in reachable, add it to reachableFor all neighbors, vj, of v, if vj is NOT in reachable
add to stack
![Page 5: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/5.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
What cities are reachable from peoria? [Just for repeatability, when I push neighbors on the stack, I do so in alphabetical order)
Stack(top->bot) Reachable
0 Peoria {}
![Page 6: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/6.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
What cities are reachable from peoria?
Stack(top->bot) Reachable
0 peoria {}
1 publeo, pittsburgh peoria
![Page 7: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/7.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Stack(top->bot) Reachable
0 peoria {}
1 pueblo, pittsburgh peoria
2 pierre, pittsburgh peoria, pueblo
![Page 8: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/8.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Stack(top->bot) Reachable
0 peoria {}
1 pueblo, pittsburgh peoria
2 pierre, pittsburgh peoria, pueblo
3 pendleton, pittsburgh peoria, pueblo, pierre
![Page 9: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/9.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Stack(top->bot) Reachable
0 peoria {}
1 pueblo, pittsburgh peoria
2 pierre, pittsburgh peoria, pueblo
3 pendleton, pittsburgh peoria, pueblo, pierre
4 phoenix, pittsburgh peoria, pueblo, pierre, pendleton
![Page 10: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/10.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Stack(top->bot) Reachable
0 peoria {}
1 pueblo, pittsburgh peoria
2 pierre, pittsburgh peoria, pueblo
3 pendleton, pittsburgh peoria, pueblo, pierre
4 phoenix, pittsburgh peoria, pueblo, pierre, pendleton
5 pittsburgh, pittsburgh peoria, pueblo, pierre, pendleton, phoenix
![Page 11: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/11.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Stack(top->bot) Reachable
0 peoria {}
1 pueblo, pittsburgh peoria
2 pierre, pittsburgh peoria, pueblo
3 pendleton, pittsburgh peoria, pueblo, pierre
4 phoenix, pittsburgh peoria, pueblo, pierre, pendleton
5 pittsburgh, pittsburgh peoria, pueblo, pierre, pendleton, phoenix
6 pensacola, pittsburgh peoria, pueblo, pierre, pendleton, phoenix, pittsburgh
![Page 12: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/12.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Stack(top->bot) Reachable
0 peoria {}
1 pueblo, pittsburgh peoria
2 pierre, pittsburgh peoria, pueblo
3 pendleton, pittsburgh
peoria, pueblo, pierre
4 phoenix, pittsburgh peoria, pueblo, pierre, pendleton
5 pittsburgh, pittsburgh
peoria, pueblo, pierre, pendleton, phoenix
6 pensacola, pittsburgh
peoria, pueblo, pierre, pendleton, phoenix, pittsburgh
7 pittsburgh peoria, pueblo, pierre, pendleton, phoenix, pittsburgh,pensacola
![Page 13: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/13.jpg)
Single-Source Reachability: Stack
Pendleton
Pierre
Pensacola
Princeton
Pittsburgh
Peoria
Pueblo
Phoenix
Stack(top->bot) Reachable
0 peoria {}
1 pueblo, pittsburgh peoria
2 pierre, pittsburgh peoria, pueblo
3 pendleton, pittsburgh
peoria, pueblo, pierre
4 phoenix, pittsburgh
peoria, pueblo, pierre, pendleton
5 pittsburgh, pittsburgh
peoria, pueblo, pierre, pendleton, phoenix
6 pensacola, pittsburgh
peoria, pueblo, pierre, pendleton, phoenix, pittsburgh
7 pittsburgh peoria, pueblo, pierre, pendleton, phoenix, pittsburgh,pensacola
8 {} peoria, pueblo, pierre, pendleton, phoenix, pittsburgh,pensacola
![Page 14: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/14.jpg)
Implementation
• Reachable: Array of integers: R[i] = 0 / 1• Stack: dynamic array deque, LL Deque• Graph Representation (edge list):– Dynamic array of LinkedLists– HashMap of LinkedLists• key = name of node• value = list of neighbors
![Page 15: CS261 Data Structures Single Source Reachability - Edge List Representation](https://reader035.vdocument.in/reader035/viewer/2022071805/56649cc55503460f9498e28f/html5/thumbnails/15.jpg)
Your Turn
• Worksheet 41• Something to think about…– What happens if we use a Queue instead of a
Stack to hold the unexplored neighbors?