5755 graphs
TRANSCRIPT
-
8/2/2019 5755 Graphs
1/58
CS 575
Design and Analysis of
Computer Algorithms
Professor Michal Cutler
Graphs and traversals
-
8/2/2019 5755 Graphs
2/58
Graph Algorithms
Graphs and Theorems aboutGraphs
Graph implementations
Graph traversals
-
8/2/2019 5755 Graphs
3/58
What can graphs model?
Cost of wiring electronic components together.
Shortest route between two cities.
Finding the shortest distance between all pairs of cities in aroad atlas.
Flow of material (liquid flowing through pipes, current throughelectrical networks, information through communicationnetworks, parts through an assembly line, etc).
State of a machine.
Used in Operating systems to model resource handling(deadlock problems).
Used in compilers for parsing and optimizing the code.
-
8/2/2019 5755 Graphs
4/58
What is a Graph?
Informally a graphis a set of nodesjoined by a set of lines or arrows.
1 12 3
4 45 56 6
2 3
-
8/2/2019 5755 Graphs
5/58
A directed graph, also called a digraphG is a pair ( V, E),where the set Vis a finite set and Eis a binary relation on V.
The set Vis called the vertex setof G and the elements are called
vertices.The set Eis called the edge setof Gand the elements are edges(alsocalled arcs).
A edge from node ato node bis denoted by the ordered pair ( a, b).
1 2 3
4 5 6
V= { 1, 2, 3, 4, 5, 6, 7 }
| V| = 7
E= { (1,2), (2,2), (2,4), (4,5), (4,1), (5,4),(6,3) }| E| = 7
Self loop
7 Isolated node
-
8/2/2019 5755 Graphs
6/58
Undirected graphG= ( V, E) , the edge set Econsistof unordered pairs. Our text uses the notation
( a,b) to refer to a directed edge, and
{ a, b} for an undirected edge.
A
D E F
B C V= { A, B, C, D, E, F }
|V| = 6
E= { {A, B}, {A,E}, {B,E}, {C,F} }|E| = 4
Some texts use (a, b) also for undirected edges.
So ( a, b) and ( b, a) refers to the same edge.
-
8/2/2019 5755 Graphs
7/58
Degreeof a Vertex in an undirected graph is the number of edges incidenton it. In a directed graph , the out degreeof a vertex is the number ofedges leaving it and the in degreeis the number of edges entering it.
A
D E F
B CThe degree of B is 2.
12
4 5
The in degree of 2 is 2 andthe out degree of 2 is 3.
Self-loop
-
8/2/2019 5755 Graphs
8/58
Simple GraphsSimple graphsare graphs without multiple
(directed in same direction or undirected)edges connecting a single pair of vertices, orself-loops. We will consider only simple
graphs.
Proposition: If G is an undirected graph then
deg(v) = 2 |E|Proposition: If G is a digraph then indeg(v) = outdeg(v) = |E|
vVvV
vV
-
8/2/2019 5755 Graphs
9/58
A weighted graphis a graph for which each edge has an associatedweight, usually given by a weight functionw: ER.
12 3
4 5 6
.5
1.2
.2
.5
1.5.3
1
4 5 6
2 32
135
-
8/2/2019 5755 Graphs
10/58
Paths
A pathis a sequence of vertices such thatthere is an edge from each vertex to its
successor. A path is simpleif each vertex is distinct.
-
8/2/2019 5755 Graphs
11/58
Cyclic and Acyclic
A path from a vertex to itself is called acycle
(e.g., v1 v2v4v1)
If a graph contains a cycle, it iscyclic
Otherwise, it isacyclic
A path is simpleif it never passes through thesame vertex twice.
1 2
4 3
-
8/2/2019 5755 Graphs
12/58
s-t connectivity problem. Given two nodes s and t, isthere a path between s and t?
s-t shortest path problem. Given two nodes s and t,
what is the length of the shortest path between s andt?
Applications. Friendster.
Maze traversal.
Kevin Bacon number.
Fewest number of hops in a communication network.
s-t Connectivity
-
8/2/2019 5755 Graphs
13/58
Connectivity
An undirected graph is connectedif any twonodes are connected by a path.
A directed graph is strongly connectedifthere is a directed path from any node to anyother node.
A graph is sparseif | E| | V|
A graph is denseif | E| | V|2
-
8/2/2019 5755 Graphs
14/58
Connected Component
Connected component. Find all nodesreachable from s.
Connected component containing node1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.
-
8/2/2019 5755 Graphs
15/58
Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring
lime pixels to blue.
Node: pixel.
Edge: two neighboring lime pixels.
Blob: connected component of lime pixels.
recolor lime green blob to blue
-
8/2/2019 5755 Graphs
16/58
Flood Fill Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
Node: pixel.
Edge: two neighboring lime pixels.
Blob: connected component of lime pixels.recolor lime green blob to blue
-
8/2/2019 5755 Graphs
17/58
A Complete graphis an undirected/directedgraph in which every pair of vertices is adjacent.If (u, v) is an edge in a graph G, we say that
vertex vis adjacentto vertex u.
A
D
E
B
4 nodes and (4*3)/2 edges
V nodes and V*(V-1)/2 edges
Note: if self loops are allowed
V*V/2
D
A
B
3 nodes and 3*2 edges
V nodes and V*(V-1) edges
Note: if self loops are allowed V2
edges
-
8/2/2019 5755 Graphs
18/58
A bipartite graphis an undirected graph G= (V,E) inwhich Vcan be partitioned into 2 sets V1 and V2 such
that ( u,v) Eimplies either uV1 and vV2 OR vV1and uV2.
-
8/2/2019 5755 Graphs
19/58
Trees
A free treeis an acyclic, connected, undirectedgraph.
A forest is an acyclic undirected graph.
A rooted treeis a tree with one distinguished node,root.
-
8/2/2019 5755 Graphs
20/58
Tree Definitions
Let G= (V, E) be an undirected graph
G is a (free) tree if:.1. Gis acyclic and connected
2. Any two vertices in Gare connected by uniquesimple path.3. Gis connected, but if any edge is removed from E,the resulting graph is disconnected.4. Gis connected, and | E| = | V| -1
5. Gis acyclic, and | E| = | V| -1G is a tree or a forest if:
Gis undirected acyclic, but if any edge is added to E,the resulting graph contains a cycle.
-
8/2/2019 5755 Graphs
21/58
Implementation of a Graph. Adjacency-list representation of a graph G =( V, E)
consists of an array ADJ of |V| lists, one for eachvertex in V. For each uV, ADJ[ u] points to all itsadjacent vertices.
1
5
1
22
5
4 4
3 3
2 5
1 5 3 4
2 4
2
4
5
1
3
2
-
8/2/2019 5755 Graphs
22/58
Adjacency-list representation
for a directed graph.
1
5
1
22
5
4 4
3 3
2 5
5 3 4
4
5
5
Variation: Can keep a second list of edges coming into a vertex.
-
8/2/2019 5755 Graphs
23/58
Adjacency lists
Advantage: Saves space for sparse graphs. Most graphs are
sparse.
Visit edges that start at v Must traverse linked list of v
Size of linked list of v is degree(v)
(degree(v)) in the worst case
Disadvantage: Check for existence of an edge (v, u)
Must traverse linked list of v
Size of linked list of v is degree(v)
(degree(v)) in the worst case
-
8/2/2019 5755 Graphs
24/58
Adjacency List
Storage We need V pointers to linked lists
For a directed graph the number of nodes (oredges) contained (referenced) in all the linkedlists is
(out-degree (v)) = | E |.
So we need ( V + E )
For an undirected graph the number of nodes is(degree (v)) = 2 | E |
Also ( V + E )
vV
vV
-
8/2/2019 5755 Graphs
25/58
Adjacency-matrix-representationof a graph G= (V, E) is a |V| x |V |matrix A such that aij = 1 if (i, j )Eand 0otherwise.
0
4
1
3
2
0 1 2 3 4
012
34
0 1 0 0 1
1 0 1 1 1
0 1 0 1 0
0 1 1 0 1
1 1 0 1 0
-
8/2/2019 5755 Graphs
26/58
Adjacency Matrix Representation for
a Directed Graph
0 1 0 0 1
0 0 1 1 1
0 0 0 1 0
0 0 0 0 1
0 0 0 0 0
0 1 2 3 4
01234
0
4
1
3
2
-
8/2/2019 5755 Graphs
27/58
Adjacency Matrix
Representation Advantage: Saves space on pointers for dense graphs
Check for existence of an edge (v, u)
(adjacency [i] [j]) == 1?) So (1)
Disadvantage:
visit all the edges that start at v Row v of the matrix must be traversed. So (|V|).
-
8/2/2019 5755 Graphs
28/58
Graph traversals
Breadth first search
Depth first search
-
8/2/2019 5755 Graphs
29/58
Some applications
Is G connected?
Does G contain a cycle?
Is G a tree?
Is G bipartite?
Find connected components
Topological sorting
Is directed G strongly connected?
-
8/2/2019 5755 Graphs
30/58
Breadth first search
Given a graph G=(V,E) and a sourcevertexs, BFS explores the edges of G
to discover (visit) each node of Greachable from s.
Idea - expand a frontierone step at a
time. Frontieris a FIFO queue (O(1) time to
update)
-
8/2/2019 5755 Graphs
31/58
Breadth first search
Computes the shortestdistance(dist) from sto any reachable node.
Computes a breadthfirsttree(of parents)with rootsthat contains all the reachablevertices from s.
To get O(|V|+|E|) we use an adjacency list
representation. If we used an adjacencymatrix it would be O(|V|2)
-
8/2/2019 5755 Graphs
32/58
Coloring the nodes
We use colors (white, grayand black)to denote the state of the node during
the search. A node is whiteif it has not been
reached (discovered).
Discoverednodes are grayor black.Graynodes are at the frontier of thesearch. Blacknodes are fully explorednodes.
-
8/2/2019 5755 Graphs
33/58
BFS - initialize
procedureBFS(G, s, color, dist, parent);
foreach vertex u do
color[u]=white; dist[u]=(V) parent[u]=-1
color[s]=gray; dist[s]=0;
init(Q); enqueue(Q, s);
-
8/2/2019 5755 Graphs
34/58
BFS - mainwhilenot (empty(Q)) do
u:=head(Q);
foreach v in adj[u] do
if (color[v]= =white) then O(E)color[v]=gray; dist[v]=dist[u]+1;
parent[v]=u; enqueue(Q, v);
dequeue(Q); color[u]=black;
endBFS
)(][deg|][|1
][
EOureeuADJ
VuVu
reachableu
Vu uADJv
-
8/2/2019 5755 Graphs
35/58
Analysis of BFS
Initialization is (|V|).
Each node can be added to the queue atmost once (it needs to be white), and its
adjacency list is searched only once. At mostall adjacency lists are searched.
If graph is undirected each edge is reachedtwice, so loop repeated at most 2|E| times.
If graph is directed each edge is reachedexactly once. So the loop repeated at most|E| times.
Worst case time O(|V|+|E|)
-
8/2/2019 5755 Graphs
36/58
BFS example
0
1
1 0
1
r s t u r s t u
v w x y v w x yr s t u r s t u
v w x y v w x y
0s w r
1 0
1
1 2
2
r t x t x v
2
22
-
8/2/2019 5755 Graphs
37/58
BFS example
2
1
1 0 2
1 2
1 2
1 2
1
2
0 2 3
1 2
r s t u r s t u
v w x y v w x y
r s t u r s t u
v w x y v w x y
1 3
2 2
0.x v u v u y
0 3
2 3
3
32
u y y
3
Now y is removed from the Q and colored red
-
8/2/2019 5755 Graphs
38/58
Depth First Search
Goal - explore every vertex and edge ofG
We go deeper whenever possible.
Directedor undirectedgraph G= (V, E).
To get (|V|+|E|) we use an adjacency
list representation. If we used anadjacency matrix it would be (|V|2)
-
8/2/2019 5755 Graphs
39/58
Depth First Search
Until there are no more undiscovered nodes.
Picks an undiscovered node and starts a depthfirst search from it.
The search proceeds from the mostrecentlydiscoverednode to discover new nodes.
When the last discovered node vis fully explored,backtracks to the node used to discover v.Eventually, the start node is fully explored.
-
8/2/2019 5755 Graphs
40/58
Depth First Search
In this version allnodes are discovered evenif the graph is directed, or undirected and notconnected
The algorithm saves: A depth first forestof the edges used to
discover new nodes.
Timestamps for the first time a node uisdiscovered d[u] and the time when thenode is fully explored f[u]
-
8/2/2019 5755 Graphs
41/58
DFS
DFS(G, color, d, f, parent);
foreach vertex u do
color[u]=white; parent[u]=-1;
(V) time=0;foreach vertex u do
if (color[u]==white) then
DFS-Visit(u)endDFS
-
8/2/2019 5755 Graphs
42/58
DFS-Visit(u)color[u]=gray; time=time+1; d[u]=time
foreach v in adj[u] do
if (color[v]==white) thenparent[v]=u; DFS-Visit(v);
color[u]=black; time=time+1;
f[u]=time;endDFS-Visit
-
8/2/2019 5755 Graphs
43/58
Analysis
DFS is (|V|) (excluding the time takenby the DFS-Visits).
DFS-Visit is called once for each nodev. Its forloop is executed |adj(v)| times.The DFS-Visit calls for all the adjacent
nodes take (|E|). Worst case time (|V|+|E|)
-
8/2/2019 5755 Graphs
44/58
DFS example (1)
x y z
u v w
1/
u v w1/
x y z
2/
u v w1/
x y z
2/
3/
u v w1/
x y z
2/
3/4/
B
-
8/2/2019 5755 Graphs
45/58
DFS example (2)
u v w
x y z
x y z
u v w
x y z4/5
1/ 2/
3/
B
u v w
4/5 3/6
1/ 2/
B
4/5 3/6
1/ 2/7
B
-
8/2/2019 5755 Graphs
46/58
DFS example (3)
u v w
x y z
u v w
x y z x y z
u v w
x y z
u v w
F
4/5 3/6
1/8 2/7
B F
4/5
9
3/6
1/8 2/7
B
F
4/5
9
3/6 10
1/8 2/7
BF
4/5
9
3/6 10/11
1/8 2/7
B
C
CC
-
8/2/2019 5755 Graphs
47/58
DFS example (4)
x y z
F
4/5
9/12
3/6 10/11
1/8 2/7
B C
u v w
-
8/2/2019 5755 Graphs
48/58
Some applications
Is undirected G connected? ChangeDFS to call dfsVisit(v) only once, and
then to check if there are still whitenodes.
O(V + E)
Find connected components. CallDFS. The nodes discovered in each callto dfsVisit(v) belong to a singlecomponent. (V+E)
-
8/2/2019 5755 Graphs
49/58
Labeling the edges
(digraph) Tree edges - those belonging to the forest
Back edges - edges from a node to anancestor in the tree.
Forward edges - a non tree edge from anode to a descendant in the tree.
Cross edges - the rest of the edges,
between trees and subtrees When a graph is undirected its edges are
tree or back edges for DFS, treeorcross
for BFS
-
8/2/2019 5755 Graphs
50/58
Classifying edges of a
digraph (u, v) is:
Tree edge if v is white
Back edge if v is gray Forward or cross - if v is black
(u, v) is:
Forward edge if v is black and d[u] < d[v] (v was
discovered after u) Cross edge if v is black and d[u] > d[v] (u
discovered after v)
-
8/2/2019 5755 Graphs
51/58
More applications
Does directed G contain a directed cycle? Do DFS ifback edges yes. Time O(V+E).
Does undirected G contain a cycle? Same as
directed but be careful not to consider (u,v) and (v, u)a cycle.
If graph is a tree or a forest it can have at most |V|-1edges. Time O(V) since encounter at most |V| edges(if (u, v) and (v, u) are counted as one edge), before
cycle is found. Is undirected G a tree? DFS with one call to
dfsVisit(v). If all vertices are reached and no backedges G is a tree. O(V)
-
8/2/2019 5755 Graphs
52/58
Directed Acyclic Graphs
Def. A topological order of a directed graph G = (V,E) is an ordering of its nodes as v1, v2, , vn so thatfor every edge (vi, vj) we have i < j.
a DAG a topological ordering
v2 v3
v6 v5 v4
v7 v1
v1 v2 v3 v4 v5 v6 v7
-
8/2/2019 5755 Graphs
53/58
Topological sort
Applications.
Course prerequisite graph: course vi
must be taken before vj. Compilation: module vimust be
compiled before vj.
Pipeline of computing jobs: output ofjob vineeded to determine input of jobvj.
-
8/2/2019 5755 Graphs
54/58
Topological sort algorithm
Given a DAG G
Topological sort is a linear ordering of all
the vertices of G such that if G contains thedirected edge (u, v) u appears before v inthe ordering
TOPOLOGICAL-SORT(G)
1. Apply DFS(G)2. As each vertex is finished insert it at the front of a
list
3. return the list
-
8/2/2019 5755 Graphs
55/58
Second algorithm
Lemma. If G is a DAG, then G has a source node.
Pf. (by contradiction)
Suppose that G is a DAG and every node has at least one incoming edge.
Pick any node v, and begin following edges backward from v. Since v has
at least one incoming edge (u, v) we can walk backward to u. Then, since u has at least one incoming edge (x, u), we can walk backward
to x.
Repeat until we visit a node, say w, twice.
Let C denote the sequence of nodes encountered between successive visitsto w. C is a cycle.
w x u v
-
8/2/2019 5755 Graphs
56/58
Second algorithm
Lemma. If G is a DAG, then G has a topological ordering.
Pf. (by induction on n)
Base case: true if n = 1.
Given DAG on n > 1 nodes, find a node v with no incoming edges.
G - { v } is a DAG, since deleting v cannot create cycles. By inductive hypothesis, G - { v } has a topological ordering.
Place v first in topological ordering; then append nodes of G - { v }in topological order.
This is valid since v has no incoming edges.
-
8/2/2019 5755 Graphs
57/58
Topological Sorting Algorithm:
Running Time Theorem. Algorithm finds a topological order in O(m
+ n) time.
Pf.
Maintain the following information: count[w] = remaining number of incoming edges
S = set of remaining nodes with no incoming edges
Initialization: O(m + n) via single scan through graph.
Update: to delete v
remove v from S decrement count[w] for all edges from v to w, and add w to S if
c count[w] hits 0
this is O(1) per edge
-
8/2/2019 5755 Graphs
58/58
Strong Connectivity:
Algorithm Theorem. Can determine if G is strongly connected in O(m + n) time.
Pf.
Pick any node s.
Run BFS from s in G.
Run BFS from s in Grev.
Return true iff all nodes reached in both BFS executions.
reverse orientation of every edge in G
strongly connected not strongly connected