data structure - datalab.snu.ac.krukang/courses/17f-ds/l23-graphs.pdf · data structure....
TRANSCRIPT
![Page 1: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/1.jpg)
U Kang 1
Data Structure
Lecture#23: Graphs(Chapter 11)
U KangSeoul National University
![Page 2: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/2.jpg)
U Kang 2
In This Lecture
Basic terms and definitions of graphs
How to represent graphs
Graph traversal methods
![Page 3: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/3.jpg)
U Kang 3
Graphs
A graph G = (V, E) consists of a set of vertices (or nodes) V, and a set of edges E, such that each edge in E is a connection between a pair of vertices in V. Example: Social network, phone call graph, computer
network, …
The number of vertices is written |V|, and the number edges is written |E|.
![Page 4: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/4.jpg)
U Kang 4
Graphs (2)
Undirected Graph Directed Graph Weighted Graph
![Page 5: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/5.jpg)
U Kang 5
Paths and Cycles
Path: A sequence of vertices v1, v2, …, vn of length n-1 with an edge from vi to vi+1 for 1 ≤ i < n. E.g., 0, 4, 1, 3, 2, 4 in the graph below is a path
A path is simple if all vertices on the path are distinct. E.g., 0, 4, 1, 3, 2 in the graph below is a simple path
![Page 6: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/6.jpg)
U Kang 6
Paths and Cycles
A cycle is a path of length 3 or more that connects vito itself. E.g., 1, 3, 2, 4, 1, 3, 2, 4, 1 in the graph below is a cycle
A cycle is simple if all vertices on the path are distinct, except the first and the last vertices E.g., 1, 3, 2, 4, 1 in the graph below is a simple cycle
![Page 7: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/7.jpg)
U Kang 7
Connected Components
An undirected graph is connected if there is at least one path from any vertex to any other.
The maximum connected subgraphs of an undirected graph are called connected components.
Connected Component 1
Connected Component 2
Connected Component 3
![Page 8: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/8.jpg)
U Kang 8
Directed Representation
AdjacencyMatrix
AdjacencyList
![Page 9: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/9.jpg)
U Kang 9
Undirected Representation
AdjacencyMatrix
AdjacencyList
![Page 10: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/10.jpg)
U Kang 10
Representation Costs
Adjacency Matrix: Θ(|V|2) space
Adjacency List: Θ(|V| + |E|) space. What is the maximum size of |E|?
Answer: |V|2
When is Adjacency List more space-efficient than Adjacency Matrix? For sparse graphs (|E| << |V|2)
When is Adjacency Matrix more space-efficient than Adjacency List? For dense graphs (|E| ~ |V|2)
![Page 11: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/11.jpg)
U Kang 11
Graph ADTinterface Graph { // Graph class ADTpublic void Init(int n); // Initializepublic int n(); // # of verticespublic int e(); // # of edgespublic int first(int v); // First neighborpublic int next(int v, int w); // Neighborpublic void setEdge(int i, int j, int wght);public void delEdge(int i, int j);public boolean isEdge(int i, int j);public int weight(int i, int j);public void setMark(int v, int val);public int getMark(int v); // Get v’s Mark
}
![Page 12: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/12.jpg)
U Kang 12
Graph Traversals
Some applications require visiting every vertex in the graph exactly once.
The application may require that vertices be visited in some special order based on graph topology.
Examples: artificial intelligence search, shortest paths problems
Important Traversals Depth First Search (DFS) Breadth First Search (BFS) Topological Sort
![Page 13: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/13.jpg)
U Kang 13
Graph Traversals (2)
To insure visiting all vertices:
void graphTraverse(Graph G) {int v;for (v=0; v<G.n(); v++)G.setMark(v, UNVISITED); // Initialize
for (v=0; v<G.n(); v++)if (G.getMark(v) == UNVISITED)doTraverse(G, v);
}
![Page 14: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/14.jpg)
U Kang 14
Depth First Search (1)
Main Idea Start from a vertex s Visit an unvisited neighbor v of s Visit an unvisited neighbor v’ of v … continue until all vertices are visited
Starting Vertex: A
![Page 15: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/15.jpg)
U Kang 15
Depth First Search (2)// Depth first search void DFS(Graph G, int v) {PreVisit(G, v); // Take appropriate actionG.setMark(v, VISITED);for (int w = G.first(v); w < G.n();
w = G.next(v, w))if (G.getMark(w) == UNVISITED)DFS(G, w);
PostVisit(G, v); // Take appropriate action}
![Page 16: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/16.jpg)
U Kang 16
Depth First Search (3)
Cost: Θ(|V| + |E|).
![Page 17: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/17.jpg)
U Kang 17
Breadth First Search (1)
Breadth First Search (BFS) Like DFS, but replace stack with a queue. Visit vertex’s neighbors before continuing deeper in
the tree.
BFS Algorithm Start from a vertex s Visit all neighbors of s Visit all neighbors of neighbors of s … continue until all vertices are visited
![Page 18: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/18.jpg)
U Kang 18
Breadth First Search (2)void BFS(Graph G, int start) {Queue<Integer> Q = new AQueue<Integer>(G.n());Q.enqueue(start);G.setMark(start, VISITED);while (Q.length() > 0) { // For each vertexint v = Q.dequeue();PreVisit(G, v); // Take appropriate actionfor (int w = G.first(v); w < G.n();
w = G.next(v, w))if (G.getMark(w) == UNVISITED) {// Put neighbors on QG.setMark(w, VISITED);Q.enqueue(w);
}PostVisit(G, v); // Take appropriate action
}}
![Page 19: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/19.jpg)
U Kang 19
Breadth First Search (3)
Cost: Θ(|V| + |E|).
1
2
3
4
56
Starting Vertex: A
![Page 20: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/20.jpg)
U Kang 20
Topological Sort (1)
Problem: Given a set of jobs, courses, etc., with prerequisite constraints, output the jobs in an order that does not violate any of the prerequisites. J2 cannot start before J1; J4 cannot start before J2 and
J3; …
![Page 21: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/21.jpg)
U Kang 21
Topological Sort (2)
May have several solutions E.g., It doesn’t matter which of J4 or J6 comes first; same for J2 or J3 J1, J3, J2, J4, J6, J5, J7 is a valid solution J1, J2, J3, J6, J4, J5, J7 is a valid solution, too
Algorithm Based on DFS Based on Queue
![Page 22: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/22.jpg)
U Kang 22
Topological Sort with DFS (1)
Main Idea (reverse topological sort) Perform DFS from each of the vertices, visiting
unvisited vertices; print out a vertex v in PostVisit for v It prints out vertices in reverse topological sort order Correctness: Assume a dependency from v1 to v2. Can
v1 be printed before v2? Why?
![Page 23: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/23.jpg)
U Kang 23
Topological Sort with DFS (2)void topsort(Graph G) {for (int i=0; i<G.n(); i++)G.setMark(i, UNVISITED);
for (int i=0; i<G.n(); i++)if (G.getMark(i) == UNVISITED)tophelp(G, i);
}
void tophelp(Graph G, int v) {G.setMark(v, VISITED);for (int w = G.first(v); w < G.n();
w = G.next(v, w))if (G.getMark(w) == UNVISITED)
tophelp(G, w);printout(v);
}
![Page 24: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/24.jpg)
U Kang 24
Topological Sort with DFS (3)
Is the order of calling vertices important? No. Why?
![Page 25: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/25.jpg)
U Kang 25
Topological Sort with Queue (1)
Main Idea Visit all edges, counting the number of incoming edges for
each vertex All vertices with no incoming edges are on the queue Process the queue
When v is taken off the queue, print v, and all outgoing neighbors of v’s counts decrement by one
Place on the queue any neighbor of v with count zero.
![Page 26: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/26.jpg)
U Kang 26
Topological Sort with Queue (2)void topsort(Graph G) {Queue<Integer> Q = new AQueue<Integer>(G.n());int[] Count = new int[G.n()];int v, w;for (v=0; v<G.n(); v++) Count[v] = 0;for (v=0; v<G.n(); v++)for (w=G.first(v); w<G.n(); w=G.next(v, w))
Count[w]++;for (v=0; v<G.n(); v++)if (Count[v] == 0) Q.enqueue(v);
while (Q.length() > 0) {v = Q.dequeue().intValue();printout(v);for (w=G.first(v); w<G.n(); w=G.next(v, w)) {
Count[w]--;if (Count[w] == 0)Q.enqueue(w);
}}
}
![Page 27: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/27.jpg)
U Kang 27
What You Need to Know
Basic terms and definitions of graphs
How to represent graphs When to use adjacency matrix or adjacency list
Graph traversal methods Main ideas and costs of DFS, BFS, and topological sort
![Page 28: Data Structure - datalab.snu.ac.krukang/courses/17F-DS/L23-graphs.pdf · Data Structure. Lecture#23: Graphs (Chapter 11) U Kang. Seoul National University. U Kang 2 In This Lecture](https://reader036.vdocuments.mx/reader036/viewer/2022070812/5f0bdea97e708231d4329e38/html5/thumbnails/28.jpg)
U Kang 28
Questions?