Graph Algorithms: CS 2110 - Fall 2019
Graph Algorithms: CS 2110 - Fall 2019
Lecture 20
CS 2110 — Fall 2019
Prelim 2 is on it's way!
2
“Graphs”, topics:
4: DAGs, topological sort
5: Planarity
6: Graph coloring
4 Sorting
CS core course prerequisites (simplified)
1110 3110
2800 4820
3410 4410
2110
1110 3110 4820
2800
B B
C C
E D D
E
DAG Not a DAG
Is this graph a DAG?
8
B D
C
Yes!
A F It was a DAG.
E
k= 0;
A 0
// inv: k nodes have been given numbers in 1..k in such a way that
B 1
if n1 <= n2, there is no edge from n2 to n1.
C
while (there is a node of in-degree 0) { D
k= 0 1
Let n be a node of in-degree 0; E
Give it number k; F
Delete n and all edges leaving it from the graph.
k= k+1;
1 0 2
} B D
2 1
JavaHyperText shows how to C
implement efficiently:
O(V+E) running time. A F
0 E 3
3 2
10 Graph Coloring
Map coloring
11
B D
A F
E
void color() { B D
for each vertex v in graph:
C
c= find_color(neighbors of v);
A F
color v with c; E
}
int find_color(vs) { Assume colors are integers 0, 1, …
int[] used;
assign used[c] the number of vertices in vs that are colored c
void color() { B D
for each vertex v in graph:
C
c= find_color(neighbors of v);
A F
color v with c; E
}
int find_color(vs) { Assume colors are integers 0, 1, …
int[] used= new int[vs.length() + 1];
for each vertex v in vs:
if color(v) <= vs.length(): If there are d
used[color(v)]++; vertices, need at
} most d+1
return smallest c such that used[c] == 0; available colors
}
Analysis
16
void color() {
for each vertex v in graph: Use the minimum
c= find_color(neighbors of v);
number of colors?
color v with c;
Maybe! Depends
}
on order vertices
int find_color(vs) {
processed.
int[] used= new int[vs.length() + 1];
for each vertex v in vs:
if color(v) <= vs.length():
used[color(v)]++;
}
return smallest c such that used[c] == 0;
}
Analysis
18
…“map-like”?
= planar
23 Planar Graphs
Planarity
24
B D
A F
E
B D
A F
E
B D
A F
E
Kuratowski's Theorem:
K5
K3,3