CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman...
-
Upload
catherine-bradford -
Category
Documents
-
view
213 -
download
0
Transcript of CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman...
![Page 1: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/1.jpg)
CSE 221: Algorithms and Data Structures
Lecture #9Graphs (with no Axes to Grind)
Steve Wolfman
2010W2
1
![Page 2: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/2.jpg)
Learning Goals
After this unit, you should be able to:• Describe the properties and possible applications of various
kinds of graphs (e.g., simple, complete), and the relationships among vertices, edges, and degrees.
• Prove basic theorems about simple graphs (e.g. handshaking theorem).
• Convert between adjacency matrices/lists and their corresponding graphs.
• Determine whether two graphs are isomorphic.• Determine whether a given graph is a subgraph of another.• Perform breadth-first and depth-first searches in graphs.• Explain why graph traversals are more complicated than tree
traversals.
2
![Page 3: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/3.jpg)
Today’s Outline
• Topological Sort: Getting to Know Graphs with a Sort• Graph ADT and Graph Representations• Graph Terminology (a lot of it!)• More Graph Algorithms
– Shortest Path (Dijkstra’s Algorithm)
– Minimum Spanning Tree (Kruskal’s Algorithm)
3
![Page 4: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/4.jpg)
Total Order
1
2
3
4
5
6
7A B means A must go before B
![Page 5: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/5.jpg)
Partial Order: Getting Dressed
socks
shirt
shoes
pants
watchbelt
coat
underroos
![Page 6: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/6.jpg)
Topological Sort
Given a graph, G = (V, E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it.
![Page 7: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/7.jpg)
Topo-Sort Take One
Label each vertex’s in-degree (# of inbound edges)
While there are vertices remaining
Pick a vertex with in-degree of zero and output it
Reduce the in-degree of all vertices adjacent to it
Remove it from the list of vertices
runtime:
![Page 8: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/8.jpg)
Topo-Sort Take Two
Label each vertex’s in-degree
Initialize a queue to contain all in-degree zero vertices
While there are vertices remaining in the queue
Pick a vertex v with in-degree of zero and output it
Reduce the in-degree of all vertices adjacent to v
Put any of these with new in-degree zero on the queue
Remove v from the queue
runtime:
![Page 9: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/9.jpg)
Today’s Outline
• Topological Sort: Getting to Know Graphs with a Sort• Graph ADT and Graph Representations• Graph Terminology (a lot of it!)• More Graph Algorithms
– Shortest Path (Dijkstra’s Algorithm)
– Minimum Spanning Tree (Kruskal’s Algorithm)
9
![Page 10: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/10.jpg)
Graph ADTGraphs are a formalism useful for representing
relationships between things– a graph G is represented as G = (V, E)• V is a set of vertices: {v1, v2, …, vn}
• E is a set of edges: {e1, e2, …, em} where each ei connects two vertices (vi1, vi2)
– operations might include:• creation (with a certain number of vertices)
• inserting/removing edges
• iterating over vertices adjacent to a specific vertex
• asking whether an edge exists connecting two vertices
Han
Leia
Luke
V = {Han, Leia, Luke}E = {(Luke, Leia), (Han, Leia), (Leia, Han)}
10
![Page 11: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/11.jpg)
Graph Applications
• Storing things that are graphs by nature– distance between cities– airline flights, travel options– relationships between people, things– distances between rooms in Clue
• Compilers– callgraph - which functions call which others– dependence graphs - which variables are defined and used at which
statements
• Others: mazes, circuits, class hierarchies, horses, networks of computers or highways or…
11
![Page 12: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/12.jpg)
Graph Representations
• 2-D matrix of vertices (marking edges in the cells)“adjacency matrix”
• List of vertices each with a list of adjacent vertices“adjacency list”
Han
Leia
Luke
12
![Page 13: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/13.jpg)
Adjacency Matrix
A |V| x |V| array in which an element (u, v) is true if and only if there is an edge from u to v
Han
Leia
Luke
Han Luke Leia
Han
Luke
Leia
runtime for various operations? space requirements: 13
![Page 14: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/14.jpg)
Adjacency List
A |V|-ary list (array) in which each entry stores a list (linked list) of all adjacent vertices
Han
Leia
LukeHan
Luke
Leia
space requirements: 14runtime for various operations?
![Page 15: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/15.jpg)
Today’s Outline
• Topological Sort: Getting to Know Graphs with a Sort• Graph ADT and Graph Representations• Graph Terminology (a lot of it!)• More Graph Algorithms
– Shortest Path (Dijkstra’s Algorithm)
– Minimum Spanning Tree (Kruskal’s Algorithm)
15
![Page 16: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/16.jpg)
Directed vs. Undirected Graphs
Han
Leia
Luke
Han
Leia
Luke
• In directed graphs, edges have a specific direction:
• In undirected graphs, they don’t (edges are two-way):
• Vertices u and v are adjacent if (u, v) E16
![Page 17: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/17.jpg)
Directed vs. Undirected Graphs
• Adjacency lists and matrices both work fine to represent directed graphs.
• To represent undirected graphs, either ensure that both orderings of every edge are included in the representation or ensure that the order doesn’t matter (e.g., always use a “canonical” order), which works poorly in adjacency lists.
17
![Page 18: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/18.jpg)
Weighted Graphs
20
30
35
60
Mukilteo
Edmonds
Seattle
Bremerton
Bainbridge
Kingston
Clinton
How can we store weights in an adjacency matrix?In an adjacency list?
Each edge has an associated weight or cost.
18
![Page 19: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/19.jpg)
Graph Density
A sparse graph has O(|V|) edges
A dense graph has (|V|2) edges
Anything in between is either on the sparse side or on the dense side, depending critically on context!
![Page 20: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/20.jpg)
Graph Density
A sparse graph has O(|V|) edges
Why is the adjacency list likely to be a better representation than the adjacency matrix for sparse graphs?
a. Sparse graphs have too few edges to fit in an adjacency matrix.
b. Much of the matrix will be “wasted” on 0s.
c. The adjacency list will guarantee better performance on a sparse graph.
d. None of these.
![Page 21: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/21.jpg)
ConnectivityUndirected graphs are connected if there is a path between
any two vertices
Directed graphs are strongly connected if there is a path from any one vertex to any other
Di-graphs are weakly connected if there is a path between any two vertices, ignoring direction
A complete graph has an edge between every pair of vertices
![Page 22: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/22.jpg)
Isomorphism and SubgraphsWe often care only about the structure of a graph, not the
names of its vertices. Then, we can ask:
“Are two graphs isomorphic?” Do the graphs have identical structure? Can you “line up” their vertices so that their edges match?
“Is one graph a subgraph of the other?” Is one graph isomorphic to a part of the other graph (a subset of its vertices and a subset of the edges connecting those vertices)?
![Page 23: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/23.jpg)
Degree
• The degree of a vertex v∈V is denoted deg(v) and represents the number of edges incident on v. (An edge from v to itself contributes 2 towards the degree.)
• Handshaking Theorem: If G=(V,E) is an undirected graph, then:
• Corollary: An undirected graph has an even number of vertices of odd degree.
23
![Page 24: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/24.jpg)
Degree/Handshake Example
• The degree of a vertex v∈V is the number of edges incident on v.
Let’s label the degree of every node and calculate the sum…
24
![Page 25: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/25.jpg)
Degree for Directed Graphs
• The in-degree of a vertex v∈V is denoted deg-(v) is the number of edges coming in to v.
• The out-degree of a vertex v∈V is denoted deg+(v) is the number of edges coming out of v.
• We let deg(v) = deg+(v) + deg-(v) • Then:
25
![Page 26: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/26.jpg)
Trees as Graphs
• Every tree is a graph with some restrictions:– the tree is directed
– there are no cycles (directed or undirected)
– there is a directed path from the root to every node
A
B
D E
C
F
HG
JI
![Page 27: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/27.jpg)
Directed Acyclic Graphs (DAGs)
DAGs are directed graphs with no cycles.
main()
add()
access()
mult()
read()
Trees DAGs Graphs We can only topo-sort DAGs!
![Page 28: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/28.jpg)
Today’s Outline
• Topological Sort: Getting to Know Graphs with a Sort• Graph ADT and Graph Representations• Graph Terminology (a lot of it!)• More Graph Algorithms
– Shortest Path (Dijkstra’s Algorithm)
– Minimum Spanning Tree (Kruskal’s Algorithm)
28
![Page 29: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/29.jpg)
Single Source, Shortest Path
Given a graph G = (V, E) and a vertex s V, find the shortest path from s to every vertex in V
Many variations:– weighted vs. unweighted
– cyclic vs. acyclic
– positive weights only vs. negative weights allowed
– multiple weight types to optimize
![Page 30: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/30.jpg)
Partial map of the Internet, 2005, by the Opte Project. (30% of Class C Networks. Line length ~ delay.)
30
![Page 31: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/31.jpg)
The Trouble with Negative Weighted Cycles
A B
C D
E
2 10
1-5
2
What’s the shortest path from A to E?(or to B, C, or D, for that matter)
![Page 32: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/32.jpg)
Unweighted Shortest Path Problem
Assume source vertex is C…
A
C
B
D
F H
G
E
Distance to: A B C D E F G H
![Page 33: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/33.jpg)
Dijkstra
Legendary figure in computer science; died a few years ago .
Supported teaching introductory computer courses without computers (pencil and paper programming)
Supposedly wouldn’t read e-mail (until late in life); so, his staff had to print out messages and put them in his box.
![Page 34: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/34.jpg)
Dijkstra’s Algorithm for Single Source Shortest Path
• Classic algorithm for solving shortest path in weighted graphs without negative weights
• A greedy algorithm (irrevocably makes decisions without considering future consequences)
• Intuition:– shortest path from source vertex to itself is 0– cost of going to adjacent nodes is at most edge weights– cheapest of these must be shortest path to that node– update paths for new node and continue picking cheapest
path
![Page 35: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/35.jpg)
Intuition in Action
A
C
B
D
F H
G
E
2 2 3
21
1
410
8
11
94
2
7
![Page 36: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/36.jpg)
Dijkstra’s Pseudocode(actually, our pseudocode for Dijkstra’s algorithm)
Initialize the cost of each node to Initialize the cost of the source to 0
While there are unknown nodes left in the graphSelect the unknown node with the lowest cost: n
Mark n as known
For each node a which is adjacent to n
a’s cost = min(a’s old cost, n’s cost + cost of (n, a))
We can get the path from thisjust as we did for mazes!
![Page 37: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/37.jpg)
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
21
1
410
8
11
94
2
7
vertex known costABCDEFGH
![Page 38: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/38.jpg)
THE KNOWNCLOUD
G
Next shortest path from inside the known cloud
P
Better pathto the same node
The Cloud Proof
But, if the path to G is the next shortest path, the path to P must be at least as long.
So, how can the path through P to G be shorter?
Source
![Page 39: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/39.jpg)
Inside the Cloud (Proof)
Everything inside the cloud has the correct shortest path
Proof is by induction on the # of nodes in the cloud:– initial cloud is just the source with shortest path 0
– inductive step: once we prove the shortest path to G is correct, we add it to the cloud
Negative weights blow this proof away!
![Page 40: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/40.jpg)
Data Structures for Dijkstra’s Algorithm
Select the unknown node with the lowest cost
findMin/deleteMin
a’s cost = min(a’s old cost, …)
decreaseKey (i.e., change a key and fix the heap)
find by name (dictionary lookup!)
|V| times:
|E| times:
runtime:
![Page 41: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/41.jpg)
Dijkstra’s uses |V| deleteMins and |E| decreaseKeys
runtime with Fibonacci heaps:
Fibonacci Heaps
• Very cool variation on Priority Queues• Amortized O(1) time bound for decreaseKey• O(log n) time for deleteMin
![Page 42: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/42.jpg)
Today’s Outline
• Topological Sort: Getting to Know Graphs with a Sort• Graph ADT and Graph Representations• Graph Terminology (a lot of it!)• More Graph Algorithms
– Shortest Path (Dijkstra’s Algorithm)
– Minimum Spanning Tree (Kruskal’s Algorithm)
42
![Page 43: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/43.jpg)
What’s a Good Maze?
![Page 44: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/44.jpg)
The Maze Construction Problem
• Given: – collection of rooms: V– connections between rooms (initially all closed): E
• Construct a maze:– collection of rooms: V = V– designated rooms in, iV, and out, oV– collection of connections to knock down: E E
such that one unique path connects every two rooms
![Page 45: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/45.jpg)
The Middle of the Maze
• So far, a number of walls have been knocked down while others remain.
• Now, we consider the wall between A and B.
• Should we knock it down?– if A and B are otherwise
connected
– if A and B are not otherwise connected
A
B
![Page 46: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/46.jpg)
Maze Construction Algorithm
While edges remain in E Remove a random edge e = (u, v) from E If u and v have not yet been connected
- add e to E- mark u and v as connected
![Page 47: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/47.jpg)
Spanning tree: a subset of the edges from a connected graph that……touches all vertices in the graph (spans the graph)…forms a tree (is connected and contains no cycles)
Minimum spanning tree (MST): the spanning tree with the least total edge cost. Which of these is the MST?
Spanning Tree
4 7
1 5
9
2
![Page 48: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/48.jpg)
Kruskal’s Algorithm for Minimum Spanning Trees
Yet another greedy algorithm:
Initialize all vertices to unconnected
While there are still unmarked edgesPick the lowest cost edge e = (u, v) and mark it
If u and v are not already connected, add e to the minimum spanning tree and connect u and v
Sound familiar?
![Page 49: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/49.jpg)
Kruskal’s Algorithm in Action (1/5)
A
C
B
D
F H
G
E
2 2 3
21
4
10
8
194
2
7
![Page 50: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/50.jpg)
Kruskal’s Algorithm in Action (2/5)
A
C
B
D
F H
G
E
2 2 3
21
4
10
8
194
2
7
![Page 51: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/51.jpg)
Kruskal’s Algorithm in Action (3/5)
A
C
B
D
F H
G
E
2 2 3
21
4
10
8
194
2
7
![Page 52: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/52.jpg)
Kruskal’s Algorithm in Action (4/5)
A
C
B
D
F H
G
E
2 2 3
21
4
10
8
194
2
7
![Page 53: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/53.jpg)
Kruskal’s Algorithm Completed (5/5)
A
C
B
D
F H
G
E
2 2 3
21
4
10
8
194
2
7
![Page 54: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/54.jpg)
Proof of CorrectnessWe already know this finds a spanning tree.
(That was part of our definition of a good maze.)
Proof by contradiction that Kruskal’s finds the minimum:Assume another spanning tree has lower cost than Kruskal’s.
Pick an edge e1 = (u, v) in that tree that’s not in Kruskal’s.
Kruskal’s tree connected u’s and v’s sets with another edge e2.
But, e2 must have at most the same cost as e1 (or Kruskal’s would have found and used e1 first to connect u’s and v’s sets).
So, replace e2 with e1 (at best keeping the cost the same).
Repeat until the tree is identical to Kruskal’s: contradiction!
QED: Kruskal’s algorithm finds a minimum spanning tree.
Actually, this proof is not correct as it stands (though Kruskal’s is).
TODO: Replace with the forest proof?
See Wikipedia page.
![Page 55: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/55.jpg)
Data Structures for Kruskal’s Algorithm
Pick the lowest cost edge…
findMin/deleteMin
If u and v are not already connected… …connect u and v.
find representative
union
|E| times:
|E| times:
With “disjoint-set” data structure, |E|lg(|E|) runtime.
![Page 56: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/56.jpg)
Learning Goals
After this unit, you should be able to:• Describe the properties and possible applications of various
kinds of graphs (e.g., simple, complete), and the relationships among vertices, edges, and degrees.
• Prove basic theorems about simple graphs (e.g. handshaking theorem).
• Convert between adjacency matrices/lists and their corresponding graphs.
• Determine whether two graphs are isomorphic.• Determine whether a given graph is a subgraph of another.• Perform breadth-first and depth-first searches in graphs.• Explain why graph traversals are more complicated than tree
traversals.
56
![Page 57: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/57.jpg)
Coming Up
• Counting and the Final Exam!
57
![Page 58: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/58.jpg)
Some Extra Examples, etc.
58
![Page 59: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/59.jpg)
Bigger (Undirected) Formal Graph Example
59
G = <V,E>
V = vertices = {A,B,C,D,E,F,G,H,I,J,K,L}
E = edges = {(A,B),(B,C),(C,D),(D,E),(E,F),(F,G),(G,H),(H,A),(A,J),(A,G),(B,J),(K,F),(C,L),(C,I),(D,I),(D,F),(F,I),(G,K),(J,L),(J,K),(K,L),(L,I)}
(A simple graph like this one is undirected, has 0 or 1 edge between each pair of vertices, and no edge from a vertex to itself.)
![Page 60: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/60.jpg)
A vertex
An edge with endpoints B and C
A path: A to B to C to D
A cycle: A to B to J to A
A path is a list of vertices {v1, v2, …, vn} such that (vi, vi+1) E for all 0 i < n.
A cycle is a path that starts and ends at the same vertex.
![Page 61: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/61.jpg)
61
ExampleV = {A, B, C, D, E}E = {{A, B}, {A, D}, {C, E}, {D, E}}
![Page 62: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/62.jpg)
62
A Directed GraphV = {A, B, C, D, E}E = {(A, B), (B, A), (B, E), (D, A), (E, A), (E, D), (E, C)}
![Page 63: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/63.jpg)
63
Weighted Graph
![Page 64: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/64.jpg)
64
Example of a Path
![Page 65: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/65.jpg)
65
Example of a Cycle
![Page 66: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/66.jpg)
66
Disconnected Graph
![Page 67: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/67.jpg)
67
Graph IsomorphismThe numbering of the vertices, and their physical arrangement are not important. The following is the same graph as the previous slide.
![Page 68: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/68.jpg)
68
Adjacency Matrix
![Page 69: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/69.jpg)
69
Adjacency Matrix (directed graph)
![Page 70: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/70.jpg)
70
Adjacency List (Directed Graph)
![Page 71: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/71.jpg)
71
Adjacency List Representation
![Page 72: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/72.jpg)
72
Breadth First Search
• Starting at a source vertex• Systematically explore the edges to “discover”
every vertex reachable from s.• Produces a “breadth-first tree”
– Root of s
– Contains all vertices reachable from s
– Path from s to v is the shortest path
![Page 73: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/73.jpg)
73
Algorithm
1. Take a start vertex, mark it identified (color it gray), and place it into a queue.
2. While the queue is not empty1. Take a vertex, u, out of the queue (Begin visiting u)2. For all vertices v, adjacent to u,
1. If v has not been identified or visited1. Mark it identified (color it gray)2. Place it into the queue3. Add edge u, v to the Breadth First Search Tree
3. We are now done visiting u (color it black)
![Page 74: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/74.jpg)
74
Example
![Page 75: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/75.jpg)
75
![Page 76: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/76.jpg)
76
Trace of Breadth First Search
![Page 77: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/77.jpg)
77
Breadth-First Search Tree
![Page 78: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/78.jpg)
78
Application of BFS
• A Breadth First Search finds the shortest path from the start vertex to all other vertices based on number of edges.
• We can use a Breadth First Search to find the shortest path through a maze.
• This may be a more efficient solution than that found by a backtracking algorithm.
![Page 79: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/79.jpg)
79
Depth First Search
• Start at some vertex• Follow a simple path discovering new vertices
until you cannot find a new vertex.• Back-up until you can start finding new vertices.
A simple path is a path with no cycles.
![Page 80: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/80.jpg)
80
Algorithm for DFS
1. Start at vertex u. Mark it visited.2. For each vertex v adjacent to u
1. If v has not been visited1. Insert u, v into DFS tree2. Recursively apply this algorithm starting at v
3. Mark u finished.4. Note: for a Directed graph, a DFS may produce
multiple trees – this is called a DFS forest.
![Page 81: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/81.jpg)
81
DFS Example
![Page 82: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/82.jpg)
82
![Page 83: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/83.jpg)
83
Trace of Depth-First Search
![Page 84: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/84.jpg)
84
Depth-First Search Tree
![Page 85: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/85.jpg)
85
Application of DFS
• A topological sort is an ordering of the vertices such that if (u, v) is an edge in the graph, then v does not appear before u in the ordering.
• A Depth First Search can be used to determine a topological sort of a Directed Acyclic Graph (DAG)
• An application of this would be to determine an ordering of courses that is consistent with the prerequisite requirements.
![Page 86: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/86.jpg)
86
CIS Prerequisites
![Page 87: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/87.jpg)
87
Topological Sort Algorithm
• Observation: If there is an edge from u to v, then in a DFS u will have a finish time later than v.
• Perform a DFS and output the vertices in the reverse order that they’re marked as finished.
![Page 88: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/88.jpg)
88
Example of a Directed Weighted Graph
![Page 89: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/89.jpg)
89
Another Weighted Graph
![Page 90: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/90.jpg)
90
Example
![Page 91: CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.](https://reader035.fdocuments.us/reader035/viewer/2022062806/56649e315503460f94b2225f/html5/thumbnails/91.jpg)
Separate Tree and Graph Examples: Cities in Germany