Professional Documents
Culture Documents
Graphs
1
Graphs
A graph is a formalism for representing relationships among
items
Han
Very general definition Luke
Very general concept
Leia
A graph is a pair: G = (V, E) V = {Han,Leia,Luke}
A set of vertices, also known E = {(Luke,Leia),
as nodes: V = {v1,v2,…,vn} (Han,Leia),
(Leia,Han)}
A set of edges E = {e1,e2,…,em}
Each edge ei is a pair of vertices (vj,vk)
An edge "connects" the vertices
A
C
Thus, (u, v) ∊ E implies (v, u) ∊ E.
Only one of these edges needs to be in the set B
The other is implicit, so normalize how you check for it
3
Directed Graphs
In directed graphs (or digraphs), edges have direction
D
D
A A
C or C
B B
2 edges here
4
More Notation D
Minimum? V = {A, B, C, D}
Maximum for undirected? E = {(C, B), (A, B),
Maximum for directed? (B, A), (C, D)}
Minimum? 0
Maximum for undirected? |V||V+1|/2 O(|V|2)
Maximum for directed? |V|2 O(|V|2)
Clinton 20
Mukilteo
Kingston 30 Edmonds
Bainbridge 35
Seattle
60
Bremerton
7
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)
Chicago
Seattle
San Francisco
Dallas
Example path (that also happens to be a cycle):
[Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
8
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]
3.5 Chicago
Seattle
2 2 length(P) = 5
cost(P) = 11.5
2 Salt Lake City
2.5
2.5 2.5
3
San Francisco Dallas
9
Paths and Cycles in Directed Graphs
Example: D
C
A
10
Undirected Graph Connectivity
An undirected graph is connected if for all
pairs of vertices u≠v, there exists a path from u to v
Connected graph
An undirected graph is complete,Disconnected graph
or fully connected, if for all pairs
of vertices u≠v there exists an
edge from u to v
11
Directed Graph Connectivity
A directed graph is strongly connected
if there is a path from every vertex to
every other vertex
12
Trees as Graphs
When talking about graphs, we
say a tree is a graph that is: D E
undirected B
acyclic
A
connected
C
All trees are graphs, but NOT all
graphs are trees F
G H
13
Rooted Trees
We are more accustomed to rooted trees where:
We identify a unique root
We think of edges as directed: parent to children
rooted tree B
A
The tree is simply drawn A
differently and with B C
undirected edges C
F D E F
G H G H
14
Rooted Trees
We are more accustomed to rooted trees where:
We identify a unique root
We think of edges as directed: parent to children
G H D E
15
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
16
Density / Sparsity
|E| is often much smaller than its maximum size
17
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"
18
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
D A B C D
A A F T F F
C
B T F F F
B
C F T F T
D F F F F
19
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: A B C D
A F T F F
Space requirements: B T F F F
C F T F T
D F F F F
Best for sparse or dense graphs?
20
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)
A B C D
Delete an edge: O(1)
A F T F F
B T F F F
Space requirements:
O(|V|2) C F T F T
D F F F F
Best for sparse or dense graphs? dense
21
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?
A B C D
A F T F F
B T F F F
C F T F T
D F F T F
But how would you "get all neighbors"?
22
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
23
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)
D
A B /
A
C
B A /
B
C D B /
D /
24
Adjacency List Properties
Running time to:
Get a vertex’s out-edges: A B /
Insert an edge: D /
Delete an edge:
Space requirements:
25
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"
A B /
D
A B C / A /
C
B C D B /
D / C /
26
Application: Moving Around WA State
27
Application: Reliability of Communication
28
Application: Reliability of Communication
29
Applications: Bus Routes Downtown
Related Problems:
Is an undirected graph connected?
Is a digraph weakly/strongly connected?
For strongly, need a cycle back to starting node
31
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"
32
Graph searching
Problem: find a path between two nodes of the
graph (e.g., Addis ababa and Bishoftu)
Methods: Depth-First-Search (DFS) or Breadth-
First-Search (BFS)
33
Breadth-First-Searching (BFS)
What is the idea behind BFS?
Look at all possible paths at the same depth
before you go at a deeper level
Back up as far as possible when you reach a
"dead end" (i.e., next vertex has been "marked"
or there is no next vertex)
BFS can be implemented efficiently using a
queue
34
Depth-First-Search (DFS)
What is the idea behind DFS?
Travel as far as you can down a path
Back up as little as possible when you reach a "dead end"
(i.e., next vertex has been "marked" or there is no next
vertex)
DFS can be implemented efficiently using a stack
35
Examples
Examples are given in the handwritten document.
36