SlideShare a Scribd company logo
1 of 92
CSE 332 Data Abstractions:
Graphs and Graph Traversals
Kate Deibel
Summer 2012
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 1
GRADES, MIDTERMS, AND IT
SEEMED LIKE A GOOD IDEA
Some course stuff and a humorous story
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 2
The Midterm
It was too long—I admit that
If it helps, this was the first exam I have
ever written
Even still, I apologize
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 3
You All Did Great
I am more than pleased with your
performances on the midterm
The points you missed were clearly due to
time constraints and stresses
You showed me you know the material
Good job!
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 4
How Grades are Calculated
Many (if not most) CSE major courses use
curving to determine final grades
 Homework and exam grades are used as
indicators and are adjusted as necessary
 Example:
A student who does excellent on
homework and projects (and goes
beyond) will get a grade bumped up
even if his/her exam scores are poorer
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 5
My Experiences as a Teacher
Timed exams are problematic
 Some of the best students I have known did
not do great on exams
The more examples of student work that one
sees, the more learning becomes evident
 Even partial effort/incomplete work tells a lot
 Unfortunately, this means losing points
The above leads to missing points
 All students (even myself back in the day) care
about points
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 6
My Repeated Mistake
As a teacher, I should talk more about how
points get transformed into a final grade
I learned this lesson my first year as a TA…
… and indirectly caused the undergraduate
CSE servers to crash
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 7
It Seemed Like a Good Idea at the Time
At the annual CS education conference
(SIGCSE), there is a special panel about
teaching mistakes and learning from them
I contributed a story at SIGCSE 2009:
http://faculty.washington.edu/deibel/prese
ntations/sigcse09-good-idea/deibel-
seemed-good-idea-sigcse-2009.ppt
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 8
My Promises
I know you will miss points
If you do the work in the class and put in
the effort, you will earn more than a
passing grade
As long as you show evidence of learning,
you will earn a good grade regardless
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 9
What This Means For You
Keep up the good work
Do not obsess over points
The final will be less intense
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 10
BACK TO CSE 332 AND
GRAPH [THEORY]
That was fun but you are here for learning…
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 11
Where We Are
We have learned about the essential ADTs
and data structures:
 Regular and Circular Arrays (dynamic sizing)
 Linked Lists
 Stacks, Queues, Priority Queues
 Heaps
 Unbalanced and Balanced Search Trees
We have also learned important algorithms
 Tree traversals
 Floyd's Method
 Sorting algorithms
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 12
Where We Are Going
Less generalized data structures and ADTs
More on algorithms and related problems
that require constructing data structures
to make the solutions efficient
Topics will include:
 Graphs
 Parallelism
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 13
Graphs
A graph is a formalism for representing
relationships among items
 Very general definition
 Very general concept
A graph is a pair: G = (V, E)
 A set of vertices, also known
as nodes: V = {v1,v2,…,vn}
 A set of edges E = {e1,e2,…,em}
 Each edge ei is a pair of vertices (vj,vk)
 An edge "connects" the vertices
Graphs can be directed or undirected
Han
Leia
Luke
V = {Han,Leia,Luke}
E = {(Luke,Leia),
(Han,Leia),
(Leia,Han)}
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 14
A Graph ADT?
We can think of graphs as an ADT
 Operations would inlude isEdge(vj,vk)
 But it is unclear what the "standard operations"
would be for such an ADT
Instead we tend to develop algorithms over
graphs and then use data structures that are
efficient for those algorithms
Many important problems can be solved by:
1. Formulating them in terms of graphs
2. Applying a standard graph algorithm
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 15
Some Graphs
For each example, what are the vertices and
what are the edges?
 Web pages with links
 Facebook friends
 "Input data" for the Kevin Bacon game
 Methods in a program that call each other
 Road maps
 Airline routes
 Family trees
 Course pre-requisites
Core algorithms that work across such domains
is why we are CSE
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 16
Scratching the Surface
Graphs are a powerful representation and
have been studied deeply
Graph theory is a major branch of research
in combinatorics and discrete mathematics
Every branch of computer science involves
graph theory to some extent
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 17
GRAPH TERMINOLOGY
To make formulating graphs easy and standard,
we have a lot of standard terminology for graphs
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 18
Undirected Graphs
In undirected graphs, edges have no specific
direction
 Edges are always "two-way"
Thus, (u, v) ∊ E implies (v, u) ∊ E.
 Only one of these edges needs to be in the set
 The other is implicit, so normalize how you
check for it
Degree of a vertex: number of edges
containing that vertex
 Put another way: the number of adjacent vertices
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 19
A
B
C
D
Directed Graphs
In directed graphs (or digraphs), edges have direction
Thus, (u, v) ∊ E does not imply (v, u) ∊ E.
Let (u, v)  E mean u → v
 Call u the source and v the destination
 In-Degree of a vertex: number of in-bound edges
(edges where the vertex is the destination)
 Out-Degree of a vertex: number of out-bound edges
(edges where the vertex is the source)
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 20
or
A
B
C
D
2 edges here
A
B
C
D
Self-Edges, Connectedness
A self-edge a.k.a. a loop edge is of the form (u, u)
 The use/algorithm usually dictates if a graph has:
 No self edges
 Some self edges
 All self edges
A node can have a(n) degree / in-degree / out-
degree of zero
A graph does not have to be connected
 Even if every node has non-zero degree
 More discussion of this to come
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 21
More Notation
For a graph G = (V, E):
 |V| is the number of vertices
 |E| is the number of edges
 Minimum?
 Maximum for undirected?
 Maximum for directed?
If (u, v) ∊ E , then v is a neighbor of u (i.e., v
is adjacent to u)
 Order matters for directed edges:
u is not adjacent to v unless (v, u)  E
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 22
A
B
C
V = {A, B, C, D}
E = {(C, B), (A, B),
(B, A), (C, D)}
D
More Notation
For a graph G = (V, E):
 |V| is the number of vertices
 |E| is the number of edges
 Minimum? 0
 Maximum for undirected? |V||V+1|/2  O(|V|2)
 Maximum for directed? |V|2  O(|V|2)
If (u, v) ∊ E , then v is a neighbor of u (i.e., v
is adjacent to u)
 Order matters for directed edges:
u is not adjacent to v unless (v, u)  E
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 23
A
B
C
D
Examples Again
Which would use directed edges?
Which would have self-edges?
Which could have 0-degree nodes?
 Web pages with links
 Facebook friends
 "Input data" for the Kevin Bacon game
 Methods in a program that call each other
 Road maps
 Airline routes
 Family trees
 Course pre-requisites
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 24
Weighted Graphs
In a weighted graph, each edge has a weight or cost
 Typically numeric (ints, decimals, doubles, etc.)
 Orthogonal to whether graph is directed
 Some graphs allow negative weights; many do not
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 25
20
30
35
60
Mukilteo
Edmonds
Seattle
Bremerton
Bainbridge
Kingston
Clinton
Examples Again
What, if anything, might weights represent for
each of these?
Do negative weights make sense?
 Web pages with links
 Facebook friends
 "Input data" for the Kevin Bacon game
 Methods in a program that call each other
 Road maps
 Airline routes
 Family trees
 Course pre-requisites
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 26
Paths and Cycles
We say "a path exists from v0 to vn" if there is a
list of vertices [v0, v1, …, vn] such that (vi,vi+1) ∊ E for
all 0  i<n.
A cycle is a path that begins and ends at the
same node (v0==vn)
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 27
Seattle
San Francisco
Dallas
Chicago
Salt Lake City
Example path (that also happens to be a cycle):
[Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
Path Length and Cost
Path length: Number of edges in a path
Path cost: Sum of the weights of each edge
Example where
P= [Seattle, Salt Lake City, Chicago, Dallas,
San Francisco, Seattle]
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 28
Seattle
San Francisco Dallas
Chicago
Salt Lake City
3.5
2 2
2.5
3
2
2.5
2.5
length(P) = 5
cost(P) = 11.5
Length is sometimes
called "unweighted cost"
Simple Paths and Cycles
A simple path repeats no vertices (except the
first might be the last):
[Seattle, Salt Lake City, San Francisco, Dallas]
[Seattle, Salt Lake City, San Francisco, Dallas, Seattle]
A cycle is a path that ends where it begins:
[Seattle, Salt Lake City, Seattle, Dallas, Seattle]
A simple cycle is a cycle and a simple path:
[Seattle, Salt Lake City, San Francisco, Dallas, Seattle]
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 29
Paths and Cycles in Directed Graphs
Example:
 Is there a path from A to D?
 Does the graph contain any cycles?
No
No
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 30
A
B
C
D
Undirected Graph Connectivity
An undirected graph is connected if for all
pairs of vertices u≠v, there exists a path
from u to v
An undirected graph is complete,
or fully connected, if for all pairs
of vertices u≠v there exists an
edge from u to v
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 31
Connected graph Disconnected graph
Directed Graph Connectivity
A directed graph is strongly connected
if there is a path from every vertex to
every other vertex
A directed graph is weakly connected
if there is a path from every vertex to
every other vertex ignoring direction
of edges
A direct graph is complete or fully
connected, if for all pairs of vertices
u≠v , there exists an edge from u to v
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 32
Examples Again
For undirected graphs: connected?
For directed graphs: strongly connected?
weakly connected?
 Web pages with links
 Facebook friends
 "Input data" for the Kevin Bacon game
 Methods in a program that call each other
 Road maps
 Airline routes
 Family trees
 Course pre-requisites
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 33
Trees as Graphs
When talking about graphs, we
say a tree is a graph that is:
 undirected
 acyclic
 connected
All trees are graphs, but NOT all
graphs are trees
How does this relate to the trees
we know and "love"?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 34
A
B
D E
C
F
H
G
Rooted Trees
We are more accustomed to rooted trees where:
 We identify a unique root
 We think of edges as directed: parent to
children
Picking a root gives a unique
rooted tree
 The tree is simply drawn
differently and with
undirected edges
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 35
A
B
D E
C
F
H
G
A
B
D E
C
F
H
G
Rooted Trees
We are more accustomed to rooted trees where:
 We identify a unique root
 We think of edges as directed: parent to
children
Picking a root gives a unique
rooted tree
 The tree is simply drawn
differently and with
undirected edges
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 36
A
B
D E
C
F
H
G
F
G H C
A
B
D E
Directed Acyclic Graphs (DAGs)
A DAG is a directed graph with no directed cycles
 Every rooted directed tree is a DAG
 But not every DAG is a rooted directed tree
 Every DAG is a directed graph
 But not every directed graph is a DAG
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 37
Examples Again
Which of our directed-graph examples do you
expect to be a DAG?
 Web pages with links
 Facebook friends
 "Input data" for the Kevin Bacon game
 Methods in a program that call each other
 Road maps
 Airline routes
 Family trees
 Course pre-requisites
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 38
Density / Sparsity
Recall:
In an undirected graph, 0≤|E|< |V|2
Recall:
In a directed graph, 0≤|E|≤|V|2
So for any graph, |E| is O(|V|2)
Another fact:
If an undirected graph is connected,
then |E| ≥ |V|-1 (pigeonhole principle)
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 39
Density / Sparsity
|E| is often much smaller than its maximum size
We do not always approximate as |E| as O(|V|2)
 This is a correct bound, but often not tight
If |E| is (|V|2) (the bound is tight), we say the
graph is dense
 More sloppily, dense means "lots of edges"
If |E| is O(|V|) we say the graph is sparse
 More sloppily, sparse means "most possible
edges missing"
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 40
GRAPH DATA STRUCTURES
Insert humorous statement here
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 41
What’s the Data Structure?
Graphs are often useful for lots of data and questions
 Example: "What’s the lowest-cost path from x to y"
But we need a data structure that represents graphs
Which data structure is "best" can depend on:
 properties of the graph (e.g., dense versus sparse)
 the common queries about the graph ("is (u ,v) an
edge?" vs "what are the neighbors of node u?")
We will discuss two standard graph representations
 Adjacency Matrix and Adjacency List
 Different trade-offs, particularly time versus space
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 42
Adjacency Matrix
Assign each node a number from 0 to |V|-1
A |V| x |V| matrix of Booleans (or 0 vs. 1)
 Then M[u][v] == true means there is an
edge from u to v
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 43
A
B
C
D A B C D
A F T F F
B T F F F
C F T F T
D F F F F
Adjacency Matrix Properties
Running time to:
 Get a vertex’s out-edges:
 Get a vertex’s in-edges:
 Decide if some edge exists:
 Insert an edge:
 Delete an edge:
Space requirements:
Best for sparse or dense graphs?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 44
A B C D
A F T F F
B T F F F
C F T F T
D F F F F
Adjacency Matrix Properties
Running time to:
 Get a vertex’s out-edges: O(|V|)
 Get a vertex’s in-edges: O(|V|)
 Decide if some edge exists: O(1)
 Insert an edge: O(1)
 Delete an edge: O(1)
Space requirements:
O(|V|2)
Best for sparse or dense graphs? dense
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 45
A B C D
A F T F F
B T F F F
C F T F T
D F F F F
Adjacency Matrix Properties
How will the adjacency matrix vary for an
undirected graph?
 Will be symmetric about diagonal axis
 Matrix: Could we save space by using only
about half the array?
 But how would you "get all neighbors"?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 46
A B C D
A F T F F
B T F F F
C F T F T
D F F T F
Adjacency Matrix Properties
How can we adapt the representation for
weighted graphs?
 Instead of Boolean, store a number in each cell
 Need some value to represent ‘not an edge’
 0, -1, or some other value based on how you
are using the graph
 Might need to be a separate field if no
restrictions on weights
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 47
Adjacency List
Assign each node a number from 0 to |V|-1
 An array of length |V| in which each
entry stores a list of all adjacent vertices
(e.g., linked list)
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 48
A
B
C
D
A
B
C
D
B /
A /
B /
/
D
Adjacency List Properties
Running time to:
 Get a vertex’s out-edges:
 Get a vertex’s in-edges:
 Decide if some edge exists:
 Insert an edge:
 Delete an edge:
Space requirements:
Best for sparse or dense graphs?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 49
A
B
C
D
B /
A /
B /
/
D
Adjacency List Properties
Running time to:
 Get a vertex’s out-edges:
O(d) where d is out-degree of vertex
 Get a vertex’s in-edges:
O(|E|) (could keep a second adjacency list for this!)
 Decide if some edge exists:
O(d) where d is out-degree of source
 Insert an edge:
O(1) (unless you need to check if it’s already there)
 Delete an edge:
O(d) where d is out-degree of source
Space requirements: O(|V|+|E|)
Best for sparse or dense graphs? sparse
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 50
Undirected Graphs
Adjacency lists also work well for
undirected graphs with one caveat
 Put each edge in two lists to support
efficient "get all neighbors"
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 51
A
B
C
D
A
B
C
D
B /
C /
B /
/
D
C /
A /
Which is better?
Graphs are often sparse
 Streets form grids
 Airlines rarely fly to all cities
Adjacency lists should generally be your
default choice
 Slower performance compensated by
greater space savings
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 52
APPLICATIONS OF
GRAPHS: TRAVERSALS
Might be easier to list what isn't a graph application…
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 53
Application: Moving Around WA State
What’s the shortest way to get from
Seattle to Pullman?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 54
Application: Moving Around WA State
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 55
What’s the fastest way to get from
Seattle to Pullman?
Application: Reliability of Communication
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 56
If Wenatchee’s phone exchange goes down,
can Seattle still talk to Pullman?
Application: Reliability of Communication
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 57
If Tacomas’s phone exchange goes down,
can Olympia still talk to Spokane?
Applications: Bus Routes Downtown
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 58
If we’re at 3rd and Pine, how can we get to
1st and University using Metro?
How about 4th and Seneca?
Graph Traversals
For an arbitrary graph and a starting node v,
find all nodes reachable from v (i.e., there
exists a path)
 Possibly "do something" for each node (print to
output, set some field, return from iterator, etc.)
Related Problems:
 Is an undirected graph connected?
 Is a digraph weakly/strongly connected?
 For strongly, need a cycle back to starting node
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 59
Graph Traversals
Basic Algorithm for Traversals:
 Select a starting node
 Make a set of nodes adjacent to current node
 Visit each node in the set but "mark" each
nodes after visiting them so you don't revisit
them (and eventually stop)
 Repeat above but skip "marked nodes"
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 60
In Rough Code Form
traverseGraph(Node start) {
Set pending = emptySet();
pending.add(start)
mark start as visited
while(pending is not empty) {
next = pending.remove()
for each node u adjacent to next
if(u is not marked) {
mark u
pending.add(u)
}
}
}
}
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 61
Running Time and Options
Assuming add and remove are O(1), entire
traversal is O(|E|) if using an adjacency list
The order we traverse depends entirely on how
add and remove work/are implemented
 DFS: a stack "depth-first graph search"
 BFS: a queue "breadth-first graph search"
DFS and BFS are "big ideas" in computer science
 Depth: recursively explore one part before going
back to the other parts not yet explored
 Breadth: Explore areas closer to start node first
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 62
Recursive DFS, Example with Tree
A tree is a graph and DFS and BFS are particularly
easy to "see" in one
Order processed: A, B, D, E, C, F, G, H
 This is a "pre-order traversal" for trees
 The marking is unneeded here but because we
support arbitrary graphs, we need a means to
process each node exactly once
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 63
A
B
D E
C
F
H
G
DFS(Node start) {
mark and process start
for each node u adjacent to start
if u is not marked
DFS(u)
}
DFS with Stack, Example with Tree
Order processed: A, C, F, H, G, B, E, D
 A different order but still a perfectly fine
traversal of the graph
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 64
A
B
D E
C
F
H
G
DFS2(Node start) {
initialize stack s to hold start
mark start as visited
while(s is not empty) {
next = s.pop() // and "process"
for each node u adjacent to next
if(u is not marked)
mark u and push onto s
}
}
BFS with Queue, Example with Tree
Order processed: A, B, C, D, E, F, G, H
 A "level-order" traversal
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 65
A
B
D E
C
F
H
G
BFS(Node start) {
initialize queue q to hold 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
}
}
DFS/BFS Comparison
BFS always finds the shortest path (or
"optimal solution") from the starting node
to a target node
 Storage for BFS can be extremely large
 A k-nary tree of height h could result in a
queue size of kh
DFS can use less space in finding a path
 If longest path in the graph is p and highest
out-degree is d then DFS stack never has more
than d⋅p elements
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 66
Implications
For large graphs, DFS is hugely more
memory efficient, if we can limit the
maximum path length to some fixed d.
If we knew the distance from the start to the
goal in advance, we could simply not add any
children to stack after level d
But what if we don’t know d in advance?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 67
Iterative Deepening (IDFS)
Algorithms
 Try DFS up to recursion of K levels deep.
 If fails, increment K and start the entire
search over
Performance:
 Like BFS, IDFS finds shortest paths
 Like DFS, IDFS uses less space
 Some work is repeated but minor
compared to space savings
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 68
Saving the Path
Our graph traversals can answer the standard
reachability question:
"Is there a path from node x to node y?"
But what if we want to actually output the path?
Easy:
 Store the previous node along the path:
When processing u causes us to add v to the
search, set v.path field to be u)
 When you reach the goal, follow path fields back to
where you started (and then reverse the answer)
 What's an easy way to do the reversal?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 69
A Stack!!
Example using BFS
What is a path from Seattle to Austin?
 Remember marked nodes are not re-enqueued
 Note shortest paths may not be unique
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 70
Seattle
San Francisco
Dallas
Salt Lake City
Chicago
Austin
1
1
1
2
3
0
Topological Sort
Problem: Given a DAG G=(V, E), output all the
vertices in order such that if no vertex appears
before any other vertex that has an edge to it
Example input:
Example output:
 142, 126, 143, 311, 331, 332, 312, 341, 351,
333, 440, 352
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 71
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Disclaimer: Do not use for official advising purposes!
(Implies that CSE 332 is a pre-req for CSE 312 – not true)
Questions and Comments
Terminology:
A DAG represents a partial order and a topological
sort produces a total order that is consistent with it
Why do we perform topological sorts only on DAGs?
 Because a cycle means there is no correct answer
Is there always a unique answer?
 No, there can be one or more answers depending
on the provided graph
What DAGs have exactly 1 answer?
 Lists
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 72
Uses Topological Sort
Figuring out how to finish your degree
Computing the order in which to
recalculate cells in a spreadsheet
Determining the order to compile files with
dependencies
In general, use a dependency graph to
find an allowed order of execution
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 73
Topological Sort: First Approach
1. Label each vertex with its in-degree
 Think "write in a field in the vertex"
 You could also do this with a data structure on
the side
2. While there are vertices not yet outputted:
a) Choose a vertex v labeled with in-degree of 0
b) Output v and "remove it" from the graph
c) For each vertex u adjacent to v, decrement in-
degree of u
- (i.e., u such that (v,u) is in E)
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 74
Example
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 75
Output:
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg:
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 76
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 77
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 78
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 79
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 0 0 0
0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 80
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 81
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 82
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
332
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 83
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
332
312
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 84
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
332
312
341
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 85
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
332
312
341
351
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 86
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
332
312
341
351
333
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 87
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
332
312
341
351
333
352
Example
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 88
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
CSE 142 CSE 143
CSE 331
CSE 311
CSE 351 CSE 333
CSE 332
CSE 341
CSE 312
CSE 352
MATH
126
CSE 440
…
Output:
126
142
143
311
331
332
312
341
351
333
352
440
Running Time?
What is the worst-case running time?
 Initialization O(|V| + |E|) (assuming adjacency list)
 Sum of all find-new-vertex O(|V|2) (because each O(|V|))
 Sum of all decrements O(|E|) (assuming adjacency list)
 So total is O(|V|2 + |E|) – not good for a sparse graph!
labelEachVertexWithItsInDegree();
for(i=0; i < numVertices; i++) {
v = findNewVertexOfDegreeZero();
put v next in output
for each w adjacent to v
w.indegree--;
}
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 89
Doing Better
Avoid searching for a zero-degree node every time!
 Keep the “pending” zero-degree nodes in a list, stack, queue,
bag, or something that gives O(1) add/remove
 Order we process them affects the output but not
correctness or efficiency
Using a queue:
 Label each vertex with its in-degree,
 Enqueue all 0-degree nodes
 While queue is not empty
 v = dequeue()
 Output v and remove it from the graph
 For each vertex u adjacent to v, decrement the in-degree
of u and if new degree is 0, enqueue it
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 90
Running Time?
labelAllWithIndegreesAndEnqueueZeros();
for(i=0; i < numVertices; i++) {
v = dequeue();
put v next in output
for each w adjacent to v {
w.indegree--;
if(w.indegree==0)
enqueue(w);
}
}
 Initialization: O(|V| + |E|) (assuming adjacency list)
 Sum of all enqueues and dequeues: O(|V|)
 Sum of all decrements: O(|E|) (assuming adjacency list)
 So total is O(|E| + |V|) – much better for sparse graph!
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 91
More Graph Algorithms
Finding a shortest path is one thing
 What happens when we consider
weighted edges (as in distances)?
Next time we will discuss shortest path
algorithms and the contributions of a
curmudgeonly computer scientist
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 92

More Related Content

Similar to graphs data structure and algorithm link list

Representing and visiting graphs
Representing and visiting graphsRepresenting and visiting graphs
Representing and visiting graphsFulvio Corno
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2showslidedump
 
Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptishan743441
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptTalhaFarooqui12
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 
[ICDE 2012] On Top-k Structural Similarity Search
[ICDE 2012] On Top-k Structural Similarity Search[ICDE 2012] On Top-k Structural Similarity Search
[ICDE 2012] On Top-k Structural Similarity SearchPei Lee
 
Graph theory and its applications
Graph theory and its applicationsGraph theory and its applications
Graph theory and its applicationsManikanta satyala
 
Graph Summarization with Quality Guarantees
Graph Summarization with Quality GuaranteesGraph Summarization with Quality Guarantees
Graph Summarization with Quality GuaranteesTwo Sigma
 
Ranking systems
Ranking systemsRanking systems
Ranking systemsMafer
 
Ranking systems
Ranking systemsRanking systems
Ranking systemsJoyce
 
Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...
Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...
Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...James Smith
 
Cost function
Cost functionCost function
Cost functionICAB
 
Making Sense of Bivector Addition
Making Sense of Bivector AdditionMaking Sense of Bivector Addition
Making Sense of Bivector AdditionJames Smith
 
P2-Chp12-Vectors.pptx
P2-Chp12-Vectors.pptxP2-Chp12-Vectors.pptx
P2-Chp12-Vectors.pptxStephenJude3
 

Similar to graphs data structure and algorithm link list (20)

Representing and visiting graphs
Representing and visiting graphsRepresenting and visiting graphs
Representing and visiting graphs
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
 
Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).ppt
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
lec 1-1 Vectors.pdf
lec 1-1 Vectors.pdflec 1-1 Vectors.pdf
lec 1-1 Vectors.pdf
 
[ICDE 2012] On Top-k Structural Similarity Search
[ICDE 2012] On Top-k Structural Similarity Search[ICDE 2012] On Top-k Structural Similarity Search
[ICDE 2012] On Top-k Structural Similarity Search
 
Graph theory and its applications
Graph theory and its applicationsGraph theory and its applications
Graph theory and its applications
 
Graph Summarization with Quality Guarantees
Graph Summarization with Quality GuaranteesGraph Summarization with Quality Guarantees
Graph Summarization with Quality Guarantees
 
Ranking systems
Ranking systemsRanking systems
Ranking systems
 
Ranking systems
Ranking systemsRanking systems
Ranking systems
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...
Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...
Solution of the Special Case "CLP" of the Problem of Apollonius via Vector Ro...
 
Cost function
Cost functionCost function
Cost function
 
Graphs.pdf
Graphs.pdfGraphs.pdf
Graphs.pdf
 
Making Sense of Bivector Addition
Making Sense of Bivector AdditionMaking Sense of Bivector Addition
Making Sense of Bivector Addition
 
Basics of graph
Basics of graphBasics of graph
Basics of graph
 
P2-Chp12-Vectors.pptx
P2-Chp12-Vectors.pptxP2-Chp12-Vectors.pptx
P2-Chp12-Vectors.pptx
 

More from KamranAli649587

More from KamranAli649587 (20)

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing tools
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptx
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptx
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.ppt
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.ppt
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdf
 
cluster.pptx
cluster.pptxcluster.pptx
cluster.pptx
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.ppt
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.ppt
 
null-13.pdf
null-13.pdfnull-13.pdf
null-13.pdf
 
Reaches
ReachesReaches
Reaches
 
null-6.pdf
null-6.pdfnull-6.pdf
null-6.pdf
 
null-1.pptx
null-1.pptxnull-1.pptx
null-1.pptx
 
Lect-01.ppt
Lect-01.pptLect-01.ppt
Lect-01.ppt
 
Db_05.ppt
Db_05.pptDb_05.ppt
Db_05.ppt
 
ch7.ppt
ch7.pptch7.ppt
ch7.ppt
 
lect09.ppt
lect09.pptlect09.ppt
lect09.ppt
 

Recently uploaded

Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationBoston Institute of Analytics
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 

Recently uploaded (20)

Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 

graphs data structure and algorithm link list

  • 1. CSE 332 Data Abstractions: Graphs and Graph Traversals Kate Deibel Summer 2012 July 23, 2012 CSE 332 Data Abstractions, Summer 2012 1
  • 2. GRADES, MIDTERMS, AND IT SEEMED LIKE A GOOD IDEA Some course stuff and a humorous story July 23, 2012 CSE 332 Data Abstractions, Summer 2012 2
  • 3. The Midterm It was too long—I admit that If it helps, this was the first exam I have ever written Even still, I apologize July 23, 2012 CSE 332 Data Abstractions, Summer 2012 3
  • 4. You All Did Great I am more than pleased with your performances on the midterm The points you missed were clearly due to time constraints and stresses You showed me you know the material Good job! July 23, 2012 CSE 332 Data Abstractions, Summer 2012 4
  • 5. How Grades are Calculated Many (if not most) CSE major courses use curving to determine final grades  Homework and exam grades are used as indicators and are adjusted as necessary  Example: A student who does excellent on homework and projects (and goes beyond) will get a grade bumped up even if his/her exam scores are poorer July 23, 2012 CSE 332 Data Abstractions, Summer 2012 5
  • 6. My Experiences as a Teacher Timed exams are problematic  Some of the best students I have known did not do great on exams The more examples of student work that one sees, the more learning becomes evident  Even partial effort/incomplete work tells a lot  Unfortunately, this means losing points The above leads to missing points  All students (even myself back in the day) care about points July 23, 2012 CSE 332 Data Abstractions, Summer 2012 6
  • 7. My Repeated Mistake As a teacher, I should talk more about how points get transformed into a final grade I learned this lesson my first year as a TA… … and indirectly caused the undergraduate CSE servers to crash July 23, 2012 CSE 332 Data Abstractions, Summer 2012 7
  • 8. It Seemed Like a Good Idea at the Time At the annual CS education conference (SIGCSE), there is a special panel about teaching mistakes and learning from them I contributed a story at SIGCSE 2009: http://faculty.washington.edu/deibel/prese ntations/sigcse09-good-idea/deibel- seemed-good-idea-sigcse-2009.ppt July 23, 2012 CSE 332 Data Abstractions, Summer 2012 8
  • 9. My Promises I know you will miss points If you do the work in the class and put in the effort, you will earn more than a passing grade As long as you show evidence of learning, you will earn a good grade regardless July 23, 2012 CSE 332 Data Abstractions, Summer 2012 9
  • 10. What This Means For You Keep up the good work Do not obsess over points The final will be less intense July 23, 2012 CSE 332 Data Abstractions, Summer 2012 10
  • 11. BACK TO CSE 332 AND GRAPH [THEORY] That was fun but you are here for learning… July 23, 2012 CSE 332 Data Abstractions, Summer 2012 11
  • 12. Where We Are We have learned about the essential ADTs and data structures:  Regular and Circular Arrays (dynamic sizing)  Linked Lists  Stacks, Queues, Priority Queues  Heaps  Unbalanced and Balanced Search Trees We have also learned important algorithms  Tree traversals  Floyd's Method  Sorting algorithms July 23, 2012 CSE 332 Data Abstractions, Summer 2012 12
  • 13. Where We Are Going Less generalized data structures and ADTs More on algorithms and related problems that require constructing data structures to make the solutions efficient Topics will include:  Graphs  Parallelism July 23, 2012 CSE 332 Data Abstractions, Summer 2012 13
  • 14. Graphs A graph is a formalism for representing relationships among items  Very general definition  Very general concept A graph is a pair: G = (V, E)  A set of vertices, also known as nodes: V = {v1,v2,…,vn}  A set of edges E = {e1,e2,…,em}  Each edge ei is a pair of vertices (vj,vk)  An edge "connects" the vertices Graphs can be directed or undirected Han Leia Luke V = {Han,Leia,Luke} E = {(Luke,Leia), (Han,Leia), (Leia,Han)} July 23, 2012 CSE 332 Data Abstractions, Summer 2012 14
  • 15. A Graph ADT? We can think of graphs as an ADT  Operations would inlude isEdge(vj,vk)  But it is unclear what the "standard operations" would be for such an ADT Instead we tend to develop algorithms over graphs and then use data structures that are efficient for those algorithms Many important problems can be solved by: 1. Formulating them in terms of graphs 2. Applying a standard graph algorithm July 23, 2012 CSE 332 Data Abstractions, Summer 2012 15
  • 16. Some Graphs For each example, what are the vertices and what are the edges?  Web pages with links  Facebook friends  "Input data" for the Kevin Bacon game  Methods in a program that call each other  Road maps  Airline routes  Family trees  Course pre-requisites Core algorithms that work across such domains is why we are CSE July 23, 2012 CSE 332 Data Abstractions, Summer 2012 16
  • 17. Scratching the Surface Graphs are a powerful representation and have been studied deeply Graph theory is a major branch of research in combinatorics and discrete mathematics Every branch of computer science involves graph theory to some extent July 23, 2012 CSE 332 Data Abstractions, Summer 2012 17
  • 18. GRAPH TERMINOLOGY To make formulating graphs easy and standard, we have a lot of standard terminology for graphs July 23, 2012 CSE 332 Data Abstractions, Summer 2012 18
  • 19. Undirected Graphs In undirected graphs, edges have no specific direction  Edges are always "two-way" Thus, (u, v) ∊ E implies (v, u) ∊ E.  Only one of these edges needs to be in the set  The other is implicit, so normalize how you check for it Degree of a vertex: number of edges containing that vertex  Put another way: the number of adjacent vertices July 23, 2012 CSE 332 Data Abstractions, Summer 2012 19 A B C D
  • 20. Directed Graphs In directed graphs (or digraphs), edges have direction Thus, (u, v) ∊ E does not imply (v, u) ∊ E. Let (u, v)  E mean u → v  Call u the source and v the destination  In-Degree of a vertex: number of in-bound edges (edges where the vertex is the destination)  Out-Degree of a vertex: number of out-bound edges (edges where the vertex is the source) July 23, 2012 CSE 332 Data Abstractions, Summer 2012 20 or A B C D 2 edges here A B C D
  • 21. Self-Edges, Connectedness A self-edge a.k.a. a loop edge is of the form (u, u)  The use/algorithm usually dictates if a graph has:  No self edges  Some self edges  All self edges A node can have a(n) degree / in-degree / out- degree of zero A graph does not have to be connected  Even if every node has non-zero degree  More discussion of this to come July 23, 2012 CSE 332 Data Abstractions, Summer 2012 21
  • 22. More Notation For a graph G = (V, E):  |V| is the number of vertices  |E| is the number of edges  Minimum?  Maximum for undirected?  Maximum for directed? If (u, v) ∊ E , then v is a neighbor of u (i.e., v is adjacent to u)  Order matters for directed edges: u is not adjacent to v unless (v, u)  E July 23, 2012 CSE 332 Data Abstractions, Summer 2012 22 A B C V = {A, B, C, D} E = {(C, B), (A, B), (B, A), (C, D)} D
  • 23. More Notation For a graph G = (V, E):  |V| is the number of vertices  |E| is the number of edges  Minimum? 0  Maximum for undirected? |V||V+1|/2  O(|V|2)  Maximum for directed? |V|2  O(|V|2) If (u, v) ∊ E , then v is a neighbor of u (i.e., v is adjacent to u)  Order matters for directed edges: u is not adjacent to v unless (v, u)  E July 23, 2012 CSE 332 Data Abstractions, Summer 2012 23 A B C D
  • 24. Examples Again Which would use directed edges? Which would have self-edges? Which could have 0-degree nodes?  Web pages with links  Facebook friends  "Input data" for the Kevin Bacon game  Methods in a program that call each other  Road maps  Airline routes  Family trees  Course pre-requisites July 23, 2012 CSE 332 Data Abstractions, Summer 2012 24
  • 25. Weighted Graphs In a weighted graph, each edge has a weight or cost  Typically numeric (ints, decimals, doubles, etc.)  Orthogonal to whether graph is directed  Some graphs allow negative weights; many do not July 23, 2012 CSE 332 Data Abstractions, Summer 2012 25 20 30 35 60 Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton
  • 26. Examples Again What, if anything, might weights represent for each of these? Do negative weights make sense?  Web pages with links  Facebook friends  "Input data" for the Kevin Bacon game  Methods in a program that call each other  Road maps  Airline routes  Family trees  Course pre-requisites July 23, 2012 CSE 332 Data Abstractions, Summer 2012 26
  • 27. Paths and Cycles We say "a path exists from v0 to vn" if there is a list of vertices [v0, v1, …, vn] such that (vi,vi+1) ∊ E for all 0  i<n. A cycle is a path that begins and ends at the same node (v0==vn) July 23, 2012 CSE 332 Data Abstractions, Summer 2012 27 Seattle San Francisco Dallas Chicago Salt Lake City Example path (that also happens to be a cycle): [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
  • 28. Path Length and Cost Path length: Number of edges in a path Path cost: Sum of the weights of each edge Example where P= [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle] July 23, 2012 CSE 332 Data Abstractions, Summer 2012 28 Seattle San Francisco Dallas Chicago Salt Lake City 3.5 2 2 2.5 3 2 2.5 2.5 length(P) = 5 cost(P) = 11.5 Length is sometimes called "unweighted cost"
  • 29. Simple Paths and Cycles A simple path repeats no vertices (except the first might be the last): [Seattle, Salt Lake City, San Francisco, Dallas] [Seattle, Salt Lake City, San Francisco, Dallas, Seattle] A cycle is a path that ends where it begins: [Seattle, Salt Lake City, Seattle, Dallas, Seattle] A simple cycle is a cycle and a simple path: [Seattle, Salt Lake City, San Francisco, Dallas, Seattle] July 23, 2012 CSE 332 Data Abstractions, Summer 2012 29
  • 30. Paths and Cycles in Directed Graphs Example:  Is there a path from A to D?  Does the graph contain any cycles? No No July 23, 2012 CSE 332 Data Abstractions, Summer 2012 30 A B C D
  • 31. Undirected Graph Connectivity An undirected graph is connected if for all pairs of vertices u≠v, there exists a path from u to v An undirected graph is complete, or fully connected, if for all pairs of vertices u≠v there exists an edge from u to v July 23, 2012 CSE 332 Data Abstractions, Summer 2012 31 Connected graph Disconnected graph
  • 32. Directed Graph Connectivity A directed graph is strongly connected if there is a path from every vertex to every other vertex A directed graph is weakly connected if there is a path from every vertex to every other vertex ignoring direction of edges A direct graph is complete or fully connected, if for all pairs of vertices u≠v , there exists an edge from u to v July 23, 2012 CSE 332 Data Abstractions, Summer 2012 32
  • 33. Examples Again For undirected graphs: connected? For directed graphs: strongly connected? weakly connected?  Web pages with links  Facebook friends  "Input data" for the Kevin Bacon game  Methods in a program that call each other  Road maps  Airline routes  Family trees  Course pre-requisites July 23, 2012 CSE 332 Data Abstractions, Summer 2012 33
  • 34. Trees as Graphs When talking about graphs, we say a tree is a graph that is:  undirected  acyclic  connected All trees are graphs, but NOT all graphs are trees How does this relate to the trees we know and "love"? July 23, 2012 CSE 332 Data Abstractions, Summer 2012 34 A B D E C F H G
  • 35. Rooted Trees We are more accustomed to rooted trees where:  We identify a unique root  We think of edges as directed: parent to children Picking a root gives a unique rooted tree  The tree is simply drawn differently and with undirected edges July 23, 2012 CSE 332 Data Abstractions, Summer 2012 35 A B D E C F H G A B D E C F H G
  • 36. Rooted Trees We are more accustomed to rooted trees where:  We identify a unique root  We think of edges as directed: parent to children Picking a root gives a unique rooted tree  The tree is simply drawn differently and with undirected edges July 23, 2012 CSE 332 Data Abstractions, Summer 2012 36 A B D E C F H G F G H C A B D E
  • 37. Directed Acyclic Graphs (DAGs) A DAG is a directed graph with no directed cycles  Every rooted directed tree is a DAG  But not every DAG is a rooted directed tree  Every DAG is a directed graph  But not every directed graph is a DAG July 23, 2012 CSE 332 Data Abstractions, Summer 2012 37
  • 38. Examples Again Which of our directed-graph examples do you expect to be a DAG?  Web pages with links  Facebook friends  "Input data" for the Kevin Bacon game  Methods in a program that call each other  Road maps  Airline routes  Family trees  Course pre-requisites July 23, 2012 CSE 332 Data Abstractions, Summer 2012 38
  • 39. Density / Sparsity Recall: In an undirected graph, 0≤|E|< |V|2 Recall: In a directed graph, 0≤|E|≤|V|2 So for any graph, |E| is O(|V|2) Another fact: If an undirected graph is connected, then |E| ≥ |V|-1 (pigeonhole principle) July 23, 2012 CSE 332 Data Abstractions, Summer 2012 39
  • 40. Density / Sparsity |E| is often much smaller than its maximum size We do not always approximate as |E| as O(|V|2)  This is a correct bound, but often not tight If |E| is (|V|2) (the bound is tight), we say the graph is dense  More sloppily, dense means "lots of edges" If |E| is O(|V|) we say the graph is sparse  More sloppily, sparse means "most possible edges missing" July 23, 2012 CSE 332 Data Abstractions, Summer 2012 40
  • 41. GRAPH DATA STRUCTURES Insert humorous statement here July 23, 2012 CSE 332 Data Abstractions, Summer 2012 41
  • 42. What’s the Data Structure? Graphs are often useful for lots of data and questions  Example: "What’s the lowest-cost path from x to y" But we need a data structure that represents graphs Which data structure is "best" can depend on:  properties of the graph (e.g., dense versus sparse)  the common queries about the graph ("is (u ,v) an edge?" vs "what are the neighbors of node u?") We will discuss two standard graph representations  Adjacency Matrix and Adjacency List  Different trade-offs, particularly time versus space July 23, 2012 CSE 332 Data Abstractions, Summer 2012 42
  • 43. Adjacency Matrix Assign each node a number from 0 to |V|-1 A |V| x |V| matrix of Booleans (or 0 vs. 1)  Then M[u][v] == true means there is an edge from u to v July 23, 2012 CSE 332 Data Abstractions, Summer 2012 43 A B C D A B C D A F T F F B T F F F C F T F T D F F F F
  • 44. Adjacency Matrix Properties Running time to:  Get a vertex’s out-edges:  Get a vertex’s in-edges:  Decide if some edge exists:  Insert an edge:  Delete an edge: Space requirements: Best for sparse or dense graphs? July 23, 2012 CSE 332 Data Abstractions, Summer 2012 44 A B C D A F T F F B T F F F C F T F T D F F F F
  • 45. Adjacency Matrix Properties Running time to:  Get a vertex’s out-edges: O(|V|)  Get a vertex’s in-edges: O(|V|)  Decide if some edge exists: O(1)  Insert an edge: O(1)  Delete an edge: O(1) Space requirements: O(|V|2) Best for sparse or dense graphs? dense July 23, 2012 CSE 332 Data Abstractions, Summer 2012 45 A B C D A F T F F B T F F F C F T F T D F F F F
  • 46. Adjacency Matrix Properties How will the adjacency matrix vary for an undirected graph?  Will be symmetric about diagonal axis  Matrix: Could we save space by using only about half the array?  But how would you "get all neighbors"? July 23, 2012 CSE 332 Data Abstractions, Summer 2012 46 A B C D A F T F F B T F F F C F T F T D F F T F
  • 47. Adjacency Matrix Properties How can we adapt the representation for weighted graphs?  Instead of Boolean, store a number in each cell  Need some value to represent ‘not an edge’  0, -1, or some other value based on how you are using the graph  Might need to be a separate field if no restrictions on weights July 23, 2012 CSE 332 Data Abstractions, Summer 2012 47
  • 48. Adjacency List Assign each node a number from 0 to |V|-1  An array of length |V| in which each entry stores a list of all adjacent vertices (e.g., linked list) July 23, 2012 CSE 332 Data Abstractions, Summer 2012 48 A B C D A B C D B / A / B / / D
  • 49. Adjacency List Properties Running time to:  Get a vertex’s out-edges:  Get a vertex’s in-edges:  Decide if some edge exists:  Insert an edge:  Delete an edge: Space requirements: Best for sparse or dense graphs? July 23, 2012 CSE 332 Data Abstractions, Summer 2012 49 A B C D B / A / B / / D
  • 50. Adjacency List Properties Running time to:  Get a vertex’s out-edges: O(d) where d is out-degree of vertex  Get a vertex’s in-edges: O(|E|) (could keep a second adjacency list for this!)  Decide if some edge exists: O(d) where d is out-degree of source  Insert an edge: O(1) (unless you need to check if it’s already there)  Delete an edge: O(d) where d is out-degree of source Space requirements: O(|V|+|E|) Best for sparse or dense graphs? sparse July 23, 2012 CSE 332 Data Abstractions, Summer 2012 50
  • 51. Undirected Graphs Adjacency lists also work well for undirected graphs with one caveat  Put each edge in two lists to support efficient "get all neighbors" July 23, 2012 CSE 332 Data Abstractions, Summer 2012 51 A B C D A B C D B / C / B / / D C / A /
  • 52. Which is better? Graphs are often sparse  Streets form grids  Airlines rarely fly to all cities Adjacency lists should generally be your default choice  Slower performance compensated by greater space savings July 23, 2012 CSE 332 Data Abstractions, Summer 2012 52
  • 53. APPLICATIONS OF GRAPHS: TRAVERSALS Might be easier to list what isn't a graph application… July 23, 2012 CSE 332 Data Abstractions, Summer 2012 53
  • 54. Application: Moving Around WA State What’s the shortest way to get from Seattle to Pullman? July 23, 2012 CSE 332 Data Abstractions, Summer 2012 54
  • 55. Application: Moving Around WA State July 23, 2012 CSE 332 Data Abstractions, Summer 2012 55 What’s the fastest way to get from Seattle to Pullman?
  • 56. Application: Reliability of Communication July 23, 2012 CSE 332 Data Abstractions, Summer 2012 56 If Wenatchee’s phone exchange goes down, can Seattle still talk to Pullman?
  • 57. Application: Reliability of Communication July 23, 2012 CSE 332 Data Abstractions, Summer 2012 57 If Tacomas’s phone exchange goes down, can Olympia still talk to Spokane?
  • 58. Applications: Bus Routes Downtown July 23, 2012 CSE 332 Data Abstractions, Summer 2012 58 If we’re at 3rd and Pine, how can we get to 1st and University using Metro? How about 4th and Seneca?
  • 59. Graph Traversals For an arbitrary graph and a starting node v, find all nodes reachable from v (i.e., there exists a path)  Possibly "do something" for each node (print to output, set some field, return from iterator, etc.) Related Problems:  Is an undirected graph connected?  Is a digraph weakly/strongly connected?  For strongly, need a cycle back to starting node July 23, 2012 CSE 332 Data Abstractions, Summer 2012 59
  • 60. Graph Traversals Basic Algorithm for Traversals:  Select a starting node  Make a set of nodes adjacent to current node  Visit each node in the set but "mark" each nodes after visiting them so you don't revisit them (and eventually stop)  Repeat above but skip "marked nodes" July 23, 2012 CSE 332 Data Abstractions, Summer 2012 60
  • 61. In Rough Code Form traverseGraph(Node start) { Set pending = emptySet(); pending.add(start) mark start as visited while(pending is not empty) { next = pending.remove() for each node u adjacent to next if(u is not marked) { mark u pending.add(u) } } } } July 23, 2012 CSE 332 Data Abstractions, Summer 2012 61
  • 62. Running Time and Options Assuming add and remove are O(1), entire traversal is O(|E|) if using an adjacency list The order we traverse depends entirely on how add and remove work/are implemented  DFS: a stack "depth-first graph search"  BFS: a queue "breadth-first graph search" DFS and BFS are "big ideas" in computer science  Depth: recursively explore one part before going back to the other parts not yet explored  Breadth: Explore areas closer to start node first July 23, 2012 CSE 332 Data Abstractions, Summer 2012 62
  • 63. Recursive DFS, Example with Tree A tree is a graph and DFS and BFS are particularly easy to "see" in one Order processed: A, B, D, E, C, F, G, H  This is a "pre-order traversal" for trees  The marking is unneeded here but because we support arbitrary graphs, we need a means to process each node exactly once July 23, 2012 CSE 332 Data Abstractions, Summer 2012 63 A B D E C F H G DFS(Node start) { mark and process start for each node u adjacent to start if u is not marked DFS(u) }
  • 64. DFS with Stack, Example with Tree Order processed: A, C, F, H, G, B, E, D  A different order but still a perfectly fine traversal of the graph July 23, 2012 CSE 332 Data Abstractions, Summer 2012 64 A B D E C F H G DFS2(Node start) { initialize stack s to hold start mark start as visited while(s is not empty) { next = s.pop() // and "process" for each node u adjacent to next if(u is not marked) mark u and push onto s } }
  • 65. BFS with Queue, Example with Tree Order processed: A, B, C, D, E, F, G, H  A "level-order" traversal July 23, 2012 CSE 332 Data Abstractions, Summer 2012 65 A B D E C F H G BFS(Node start) { initialize queue q to hold 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 } }
  • 66. DFS/BFS Comparison BFS always finds the shortest path (or "optimal solution") from the starting node to a target node  Storage for BFS can be extremely large  A k-nary tree of height h could result in a queue size of kh DFS can use less space in finding a path  If longest path in the graph is p and highest out-degree is d then DFS stack never has more than d⋅p elements July 23, 2012 CSE 332 Data Abstractions, Summer 2012 66
  • 67. Implications For large graphs, DFS is hugely more memory efficient, if we can limit the maximum path length to some fixed d. If we knew the distance from the start to the goal in advance, we could simply not add any children to stack after level d But what if we don’t know d in advance? July 23, 2012 CSE 332 Data Abstractions, Summer 2012 67
  • 68. Iterative Deepening (IDFS) Algorithms  Try DFS up to recursion of K levels deep.  If fails, increment K and start the entire search over Performance:  Like BFS, IDFS finds shortest paths  Like DFS, IDFS uses less space  Some work is repeated but minor compared to space savings July 23, 2012 CSE 332 Data Abstractions, Summer 2012 68
  • 69. Saving the Path Our graph traversals can answer the standard reachability question: "Is there a path from node x to node y?" But what if we want to actually output the path? Easy:  Store the previous node along the path: When processing u causes us to add v to the search, set v.path field to be u)  When you reach the goal, follow path fields back to where you started (and then reverse the answer)  What's an easy way to do the reversal? July 23, 2012 CSE 332 Data Abstractions, Summer 2012 69 A Stack!!
  • 70. Example using BFS What is a path from Seattle to Austin?  Remember marked nodes are not re-enqueued  Note shortest paths may not be unique July 23, 2012 CSE 332 Data Abstractions, Summer 2012 70 Seattle San Francisco Dallas Salt Lake City Chicago Austin 1 1 1 2 3 0
  • 71. Topological Sort Problem: Given a DAG G=(V, E), output all the vertices in order such that if no vertex appears before any other vertex that has an edge to it Example input: Example output:  142, 126, 143, 311, 331, 332, 312, 341, 351, 333, 440, 352 July 23, 2012 CSE 332 Data Abstractions, Summer 2012 71 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Disclaimer: Do not use for official advising purposes! (Implies that CSE 332 is a pre-req for CSE 312 – not true)
  • 72. Questions and Comments Terminology: A DAG represents a partial order and a topological sort produces a total order that is consistent with it Why do we perform topological sorts only on DAGs?  Because a cycle means there is no correct answer Is there always a unique answer?  No, there can be one or more answers depending on the provided graph What DAGs have exactly 1 answer?  Lists July 23, 2012 CSE 332 Data Abstractions, Summer 2012 72
  • 73. Uses Topological Sort Figuring out how to finish your degree Computing the order in which to recalculate cells in a spreadsheet Determining the order to compile files with dependencies In general, use a dependency graph to find an allowed order of execution July 23, 2012 CSE 332 Data Abstractions, Summer 2012 73
  • 74. Topological Sort: First Approach 1. Label each vertex with its in-degree  Think "write in a field in the vertex"  You could also do this with a data structure on the side 2. While there are vertices not yet outputted: a) Choose a vertex v labeled with in-degree of 0 b) Output v and "remove it" from the graph c) For each vertex u adjacent to v, decrement in- degree of u - (i.e., u such that (v,u) is in E) July 23, 2012 CSE 332 Data Abstractions, Summer 2012 74
  • 75. Example CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … July 23, 2012 CSE 332 Data Abstractions, Summer 2012 75 Output: Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? In-deg:
  • 76. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 76 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
  • 77. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 77 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126
  • 78. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 78 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142
  • 79. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 79 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143
  • 80. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 80 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311
  • 81. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 81 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331
  • 82. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 82 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331 332
  • 83. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 83 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331 332 312
  • 84. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 84 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331 332 312 341
  • 85. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 85 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331 332 312 341 351
  • 86. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 86 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331 332 312 341 351 333
  • 87. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 87 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x x x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331 332 312 341 351 333 352
  • 88. Example July 23, 2012 CSE 332 Data Abstractions, Summer 2012 88 Node: 126 142 143 311 312 331 332 333 341 351 352 440 Removed? x x x x x x x x x x x x In-deg: 0 0 2 1 2 1 1 2 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 CSE 142 CSE 143 CSE 331 CSE 311 CSE 351 CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 … Output: 126 142 143 311 331 332 312 341 351 333 352 440
  • 89. Running Time? What is the worst-case running time?  Initialization O(|V| + |E|) (assuming adjacency list)  Sum of all find-new-vertex O(|V|2) (because each O(|V|))  Sum of all decrements O(|E|) (assuming adjacency list)  So total is O(|V|2 + |E|) – not good for a sparse graph! labelEachVertexWithItsInDegree(); for(i=0; i < numVertices; i++) { v = findNewVertexOfDegreeZero(); put v next in output for each w adjacent to v w.indegree--; } July 23, 2012 CSE 332 Data Abstractions, Summer 2012 89
  • 90. Doing Better Avoid searching for a zero-degree node every time!  Keep the “pending” zero-degree nodes in a list, stack, queue, bag, or something that gives O(1) add/remove  Order we process them affects the output but not correctness or efficiency Using a queue:  Label each vertex with its in-degree,  Enqueue all 0-degree nodes  While queue is not empty  v = dequeue()  Output v and remove it from the graph  For each vertex u adjacent to v, decrement the in-degree of u and if new degree is 0, enqueue it July 23, 2012 CSE 332 Data Abstractions, Summer 2012 90
  • 91. Running Time? labelAllWithIndegreesAndEnqueueZeros(); for(i=0; i < numVertices; i++) { v = dequeue(); put v next in output for each w adjacent to v { w.indegree--; if(w.indegree==0) enqueue(w); } }  Initialization: O(|V| + |E|) (assuming adjacency list)  Sum of all enqueues and dequeues: O(|V|)  Sum of all decrements: O(|E|) (assuming adjacency list)  So total is O(|E| + |V|) – much better for sparse graph! July 23, 2012 CSE 332 Data Abstractions, Summer 2012 91
  • 92. More Graph Algorithms Finding a shortest path is one thing  What happens when we consider weighted edges (as in distances)? Next time we will discuss shortest path algorithms and the contributions of a curmudgeonly computer scientist July 23, 2012 CSE 332 Data Abstractions, Summer 2012 92