Lecture Algo
Lecture Algo
Graph Algorithms
Administrative
Test postponed to Friday
Homework:
Turned in last night by midnight: full credit
Turned in tonight by midnight: 1 day late, 10% off
Turned in tomorrow night: 2 days late, 30% off
Extra credit lateness measured separately
Review: Graphs
A graph G = (V, E)
V = set of vertices, E = set of edges
Dense graph: |E| |V|2; Sparse graph: |E| |V|
Undirected graph:
Edge (u,v) = edge (v,u)
No self-loops
Directed graph:
Edge (u,v) goes from vertex u to vertex v, notated uv
or the vertices
matrix A:
A[i, j] = 1 if edge (i, j) E (or weight of edge)
= 0 if edge (i, j) E
Storage requirements: O(V2)
A dense representation
every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forest if graph is not
connected
etc.
algorithm
White vertices have not been discovered
All vertices start out white
vertices
represent?
represent?
Q: s
Q: w
Q: r
Q:
Q: x
Q: v
Q: u
Q: y
Q:
source node
Shortest-path distance (s,v) = minimum number of
Depth-First Search
Depth-first search is another strategy for
exploring a graph
Explore deeper in the graph whenever possible
Edges are explored out of the most recently
Depth-First Search
Vertices initially colored white
Then colored gray when discovered
Then black when finished
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
graph
Runs once/edge if directed graph, twice if undirected
Thus loop will run in O(E) time, algorithm O(V+E)
Considered linear for graph, b/c adj list requires O(V+E) storage
and analysis
DFS Example
source
vertex
DFS Example
source
vertex
1 |
DFS Example
source
vertex
1 |
2 |
DFS Example
source
vertex
d
1 |
f
|
2 |
3 |
DFS Example
source
vertex
1 |
2 |
3 | 4
DFS Example
source
vertex
1 |
2 |
3 | 4
5 |
DFS Example
source
vertex
1 |
2 |
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
9 |
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |11
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
14|
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
Tree edges
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
Tree edges Back edges
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
source
F?
source
no back edges
If acyclic, no back edges (because a back edge implies a
cycle
If no back edges, acyclic
No back edges implies only tree edges (Why?)
Only tree edges implies we have a tree or a forest
Which by definition is acyclic
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
DFS_Visit(u)
{
u->color = GREY;
time = time+1;
u->d = time;
for each v u->Adj[]
{
if (v->color == WHITE)
DFS_Visit(v);
}
u->color = BLACK;
time = time+1;
u->f = time;
}
O(V) time:
In an undirected acyclic forest, |E| |V| - 1
So count the edges: if ever see |V| distinct edges,