Download presentation
Presentation is loading. Please wait.
Published byDjaja Lesmono Modified over 6 years ago
1
Ch. 14 Graphs Graph traversals Applications of graphs
Depth-first search Breadth-first search Applications of graphs Topological sorting Spanning trees Shortest paths
2
Definitions Graph G = (V, E) where Subgraph
V is the set of vertices (nodes) and E is the set of edges Subgraph A subset of a graph’s vertices and edges Two vertices are adjacent if they are joined by an edge
3
Path Cycle Simple path Simple cycle A seq. of connected edges
A path whose starting vertex and ending vertex are the same Simple path A path that contains no cycle Simple cycle A cycle that contains no cycle in it
4
Directed graph (digraph) : undirected graph
Connected graph Every pair of distinct vertices has a path bet’n them Complete graph Every pair of distinct vertices has an edge bet’n them Directed graph (digraph) : undirected graph Whether edges have direction ? Weighted graph : unweighted graph Whether edges have weights ? Adjacency, multigraph, loop
5
A campus map as a graph A subgraph
6
Graphs that are Connected Disconnected Complete
7
Multigraph Loop Usually these are not considered as graphs. They are called as a multigraph and a graph w/ loops.
8
A weighted graph
9
A directed graph ~11/10/2003 ~5/13/2009
10
Graph의 예 영희 철수 준호 동건 승우 재상 사람들간의 친분 관계를 나타낸 그래프 10
11
그래프의 예 영희 철수 준호 동건 승우 재상 9 6 5 5 7 9 1 5 친밀도를 가중치로 나타낸 친분관계 그래프 11
12
그래프의 예 Directed Graph (Digraph, 유향 그래프) 방향을 고려한 친분관계 그래프 영희 철수 준호 동건
승우 재상 방향을 고려한 친분관계 그래프 12
13
그래프의 예 영희 철수 준호 동건 승우 재상 9 6 8 8 5 5 7 9 6 2 1 5 가중치를 가진 Digraph 13
14
Graph Representation N: # of vetices
15
Adjacency Matrix Undirected Graph의 예 영희 철수 준호 동건 승우 재상 1 1 1 2 3 4 5 6
1 1 2 4 6 2 3 4 3 5 5 6 Undirected Graph의 예 15
16
Adjacency Matrix Weighted Undirected Graph의 예 영희 철수 준호 동건 승우 재상 9 7 5
1 1 2 3 4 5 6 9 7 5 6 1 9 6 1 5 2 4 6 2 5 7 3 9 1 4 3 5 5 5 6 Weighted Undirected Graph의 예 16
17
Adjacency Matrix Directed Graph의 예 영희 철수 준호 동건 승우 재상 1 1 1 2 3 4 5 6 1
1 1 2 4 6 2 3 4 3 5 5 6 Directed Graph의 예 17
18
Adjacency Matrix Weighted Digraph의 예 영희 철수 준호 동건 승우 재상 8 7 5 6 9 2 1 9
3 4 5 6 8 7 5 6 9 2 1 9 6 1 8 8 5 2 4 6 2 5 7 3 9 6 2 1 4 3 5 5 5 6 Weighted Digraph의 예 18
19
Adjacency Matrix Digraph의 다른 예 19
20
Adjacency Matrix Weighted Graph의 다른 예 20
21
Adjacency list A graph is represented by N linked lists
where list i is the list of vertices that is adjacent to vertex i. In case of weighted graphs, the list also contains the weight values.
22
Adjacency List Undirected Graph의 예 영희 철수 준호 동건 승우 재상 2 3 4 6 1 3 1 2 5
22
23
Adjacency List Weighted Graph의 예 영희 철수 준호 동건 승우 재상 9 7 2 9 3 7 4 5 6 1
23
24
A Digraph and Its Adjacency Matrix
25
A Digraph and Its Adjacency List
26
A Weighted Digraph and Its Adjacency List
~5/21/2012
27
Graph Traversals Traversal in a graph G
starts at a vertex u and visits all vertices v for which there is a path bet’n u and v. Two representatives Depth-first search Breadth-first search ~5/14/2007
28
Depth-First Search DFS (v) { Mark v as visited;
for (each unvisited vertex u adjacent to v) DFS(u); }
29
Non-Recursive Version of Depth-First Search
DFS(v) { // All vertices are marked UNVISITED in the beginning stack.push(v); mark[v] = VISITED; while (!stack.isEmpty( )) { if (no unvisited vertices are adjacent to the stack-top vertex) stack.pop( ); // backtracking else { Select an unvisited vertex w adjacent to the stack-top vertex; stack.push(w); mark[w] = VISITED; } ~11/17/2008
30
Breadth-First Search BFS(v)
{ // All vertices are marked UNVISITED in the beginning queue.enqueue(v); mark[v] = VISITED; while (!queue.isEmpty( )) { w = queue.dequeue( ); for each vertex u adjacent to w { queue.enqueue(u); mark[u] = VISITED; }
31
동일한 트리를 각각 DFS/BFS로 방문하기
32
A Connected Graph with Cycles
33
The results of a depth-first traversal, beginning at vertex a, of the graph in the previous page
34
The results of a breadth-first traversal, beginning at vertex a, of the graph in the previous page
~5/23/2018(제23강)
35
Topological Sorting Condition Topological order
Directed graph without cycles Topological order An order of vertices in which vertex x precedes vertex y if there is an edge from x to y Usually, there are many topological orders for a directed graph.
36
A graph and examples of topological orders
~11/10/2010 ~11/21/2011
37
topologicalSort (G) // G: graph { for i = 1 to G.size( ) { Select a vertex v that has no successors; vertex[i] = v; Delete from G vertex v and the edges to v; } List vertex[G.size( )],…,vertex[1]; // a topological order
38
Remove b from theGraph;
add it to aList
39
Spanning Trees Condition Tree Spanning tree of graph G
Undirected connected graph Tree A connected graph with no cycles A tree with n vertices always has n-1 edges. Spanning tree of graph G A tree as a subgraph of G that contains all of G’s vertices Mention observation ~11/19/2008
40
A Spanning Tree
41
DFS/BFS Spanning Trees
DFSTree (v) { Mark v as visited; for (each unvisited vertex u adjacent to v) { Mark the edge (u, v); DFSTree(u); } Mention observations. The edges marked in DFSTree() constitute a DFS spanning tree.
42
The edges marked in BFSTree() constitute a BFS spanning tree.
BFSTree(v) { // All vertices are marked UNVISITED in the beginning queue.enqueue(v); mark[v] = VISITED; while (!queue.isEmpty( )) { w = queue.dequeue( ); for each vertex u adjacent to w { queue.enqueue(u); mark[u] = VISITED; Mark the edge (w, u); } The edges marked in BFSTree() constitute a BFS spanning tree.
43
A DFS spanning tree rooted at vertex a
44
A BFS spanning tree rooted at vertex a
45
Minimum Spanning Trees
Condition Weighted undirected graph The cost of a spanning tree The sum of the edge weights in the spanning tree Minimum spanning tree A spanning tree with the minimum cost
46
Prim Algorithm PrimAlgorithm (v) {
Mark v as visited and include it in the m.s.t.; while (there are unvisited vertices) { Find a least-cost edge (x,u) from a visited vertex x to an unvisited vertex u; Mark u as visited; Add the vertex u and the edge (x,u) to the m.s.t.; } ~5/23/2012 Prim’s algorithm is an example of greedy algorithm. It is a rare case that a greedy algorithm guarantees global optimality.
47
A weighted, connected, undirected graph
48
Prim Algorithm의 작동 예 1
52
~5/19/2004 ~11/25/2004 ~5/21/2007 ~5/25/2009
53
Prim Algorithm의 작동 예 2 (a) (b) (c) (d) : 방금 S에 포함된 정점 : 방금 이완이 일어난 정점
8 8 8 ∞ 10 10 r ∞ ∞ 9 9 5 5 11 ∞ 9 11 13 12 13 12 ∞ ∞ ∞ 11 8 11/13 수정됨 7 8 7 ∞ ∞ (d) (c) 8 8 10 8 10 S 10 S 5 9 9 5 11 5 9 11 9 13 12 13 12 ∞ 11 12 11 8 7 ∞ 8 7 ∞ : 방금 S에 포함된 정점 : 방금 이완이 일어난 정점
54
(e) (f) 8 8 5 5 5 S 11 9 11 9 13 12 12 12 (g) 11 12 11 8 8 7 S 8 7 ∞ 8 11/13 수정됨 ~5/21/2008 10 9 (i) S (h) 12 8 8 7 11 5 7 10 8 8 9 9 7 11 7 11 7 8 8 S
55
Shortest Paths Condition Shortest path bet’n two vertices
Weighted digraph Undirected graph can be also thought to be a digraph in which undirected edge (u,v) means two directed edges (u,v) and (v,u). Shortest path bet’n two vertices A path that has the smallest sum of edge weights
56
Dijkstra Algorithm Relaxation!
DijkstraAlgorithm (G, s) // s: the starting vertex { S = {s} ; for v = 1 to G.size( ) distance[v] = weight(s,v); for i = 2 to G.size( ) { Find the smallest distance[v] s.t. v is not in S; S = S ∪{v}; for each vertex u not in S { if (distance[u] > distance[v] + weight(v,u)) distance[u] = distance[v] + weight(v,u); } Relaxation! Dijkstra’s algorithm is an example of greedy algorithm. It is another rare case that a greedy algorithm guarantees global optimality.
57
A weighted directed graph and its adjacency matrix
Manual trace. A weighted directed graph and its adjacency matrix
58
An example relaxation
59
Dijkstra Algorithm의 작동 예
∞ 8 10 8 8 10 8 10 6 ∞ ∞ 2 6 2 18 6 2 9 1 9 1 11 ∞ ∞ 11 1 ∞ 5 9 5 9 ∞ 3 12 3 12 3 12 5 ∞ ∞ ∞ 8 11 ∞ 8 11 8 8 7 4 8 7 4 7 ∞ 8 4 ∞ ∞ 8 8 8 6 10 10 6 10 2 2 1 ∞ 9 12 9 12 9 12 5 12 5 12 5 3 19 ∞ ∞ 11 11 11 8 8 8 7 8 7 4 8 7 4 8 4 19 ∞ ∞
60
8 8 6 10 10 9 12 9 12 12 5 19 19 11 8 11 8 7 4 19 16 ~11/21/2007(top. Sort와 Prim alg.과 순서 바꿈) ~11/15/2010 8 8 10 8 10 2 10 9 1 11 9 12 3 12 5 9 12 5 12 19 8 8 10 11 19 11 7 4 10 2 16 9 1 16 11 9 12 3 12 5 19 11 7 4 16
61
Euler Circuit : The Oldest Graph Application
Is there a cycle that uses every edge exactly once? To have an Euler circuit, every vertex must have an even degree.
62
Pencil and paper drawings
63
Other Graph Problems Traveling salesman problem Graph planarity
Graph coloring Graph bisection Max flow VLSI circuit placement & routing Too many more … ~11/28/2005 ~5/26/2008 ~11/24/2008 ~5/27/2009
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.