Lecture 15 - Graph Data Structures and Traversals
Lecture 15 - Graph Data Structures and Traversals
Things to address:
• Amount to write on printed slides
• Why using polling system for in-class exercises
• Concern about not getting through entire slide deck
Year in Program this Fall
Wide range of student 25
backgrounds! 20
15
Hence, using a range of 10
teaching styles, pauses, etc. 5
0
Represented majors:
• Engineering
• Math
Last Time Programmed / Taken CS Course
• Science
40
• Informatics
30
• Geology
20
• Spanish
10
• Asian Language 0
• Pre-major
• And more!
Graph Data Structures
A couple of different ways to store adjacencies
What is the Data Structure?
• So graphs are really useful for lots of data and questions
• For example, “what’s the lowest-cost path from x to y”
• Space requirements:
E
S (2)
B
(1)
• Best for sparse or dense graphs? (0)
M
(3)
Adjacency Matrix Properties
• How will the adjacency matrix vary for an undirected graph?
• Undirected will be symmetric around the diagonal
0
E
S (2)
B 1
(1)
(0) 2
M
(3) 3
Adjacency List Properties 0 /
2 3 /
1 0
2 3 /
• Running time to:
• Get all of a vertex’s out-edges: 3 1 2 /
where d is out-degree of vertex
• Get all of a vertex’s in-edges:
(but could keep a second adjacency list for this!) E
S (2)
B
• Decide if some edge exists: (1)
(0)
where d is out-degree of source M
• Insert an edge: (3)
(unless you need to check if it’s there)
• Delete an edge:
where d is out-degree of source
• Space requirements: Best for sparse or dense graphs?
Algorithms
Okay, we can represent graphs
CSE 417
One example output:
Questions and comments
• Why do we perform topological sorts only on DAGs?
2
0 4
3
• Is there always a unique answer? 1
CSE 417
Node: 126 142 143 374 373 410 413 415 417 XYZ
Removed?
In-degree: 0 0 2 1 1 1 1 1 1 3
Notice
Using a queue:
1. Label each vertex with its in-degree, enqueue 0-degree nodes
2. While queue is not empty
a) v = dequeue()
b) Output v and remove it from the graph
c) For each vertex u adjacent to v (i.e. u such that (v,u) in E), decrement the in-degree of u,
if new degree is 0, enqueue it
Example: Topological Sort Using Queues
B Node A B C D
C Removed?
A
In-degree 0 1 1 2
D
Queue: The trick is to avoid searching for a zero-degree node every time!
1. Label each vertex with its in-degree, enqueue 0-degree nodes
2. While queue is not empty
Output: a) v = dequeue()
b) Output v and remove it from the graph
c) For each vertex u adjacent to v (i.e. u such that (v,u) in E),
decrement the in-degree of u, if new degree is 0, enqueue it
Running time?
labelAllAndEnqueueZeros();
for(i=0; ctr < numVertices; ctr++){
v = dequeue();
put v next in output
for each u adjacent to v {
u.indegree--;
if(u.indegree==0)
enqueue(u);
}
}
• What is the worst-case running time?
• Initialization: (assuming adjacency list)
• Sum of all enqueues and dequeues:
• Sum of all decrements: (assuming adjacency list)
• Total: – much better for sparse graph!
Graph Traversals
Depth- and Breadth- First Searches!
Introductory Example: Graph Traversals
How would a computer systematically find a path through the maze?
Source
A B C D E
F G H I J
K L M N O
Destination
Note: under the hood, we’re using a graph to represent the maze
In graph terminology: find a path (if any) from one vertex to another.
Source
Source
A B C D E A B C D E
F G H I J F G H I J
K L M N O K L M N O
Destination Destination
Find a path (if any) from one vertex to another.
Source
A B C D E
F G H I J
K L M N O
Destination
Depth First Search (DFS)
Depth First Search (DFS):
Explore as far as possible along each branch before backtracking
Repeatedly explore adjacent vertices using or
Mark each vertex we visit, so we don’t process each more than once.
Source
A B C D E
F G H I J
K L M N O
Destination
Breadth First Search (BFS)
Breadth First Search (BFS):
Explore neighbors first, before moving to the next level of neighbors.
Repeatedly explore adjacent vertices using
Mark each vertex we visit, so we don’t process each more than once.
BFS(Node start) {
Example pseudocode: initialize queue q and enqueue start
mark start as visited
while(q is not empty) {
next = q.dequeue() // and “process”
for each node u adjacent to next
if(u is not marked)
mark u and enqueue onto q
}
}
Practice time!
What is one possible order of visiting the nodes of the following graph
when using Breadth First Search (BFS)?
M N O
R Q P
A) MNOPQR C) QMNPRO
B) NQMPOR D) QMNPOR
(space for scratch work / notes)
Running Time and Traversal Order
• Assuming add and remove are O(1), entire traversal is
• Use an adjacency list representation
• A third approach:
• Iterative deepening (IDFS):
• Try DFS but disallow recursion more than K levels deep
• If that fails, increment K and start the entire search over
• Like BFS, finds shortest paths. Like DFS, less space.
Graph Traversal Uses
In addition to finding paths, we can use graph traversals to answer:
• What are all the vertices reachable from a starting vertex?
• Is an undirected graph connected?
• Is a directed graph strongly connected?
• How to do it:
• Instead of just “marking” a node, store the previous node along the path
• When you reach the goal, follow path fields back to where you started (and then
reverse the answer)
• If just wanted path length, could put the integer distance at each node instead once
Saving the Path
• Our graph traversals can answer the reachability question:
• “Is there a path from node x to node y?”
• How to do it:
• Instead of just “marking” a node, store the previous node along the path
• When you reach the goal, follow path fields back to where you started (and
then reverse the answer)
• If just wanted path length, could put the integer distance at each node
instead
Source:
https://xkcd.com/761/