0% found this document useful (0 votes)
56 views

Graph Algorithms: CS 2110 - Fall 2019

Uploaded by

C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Graph Algorithms: CS 2110 - Fall 2019

Uploaded by

C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

GRAPH ALGORITHMS

Lecture 20
CS 2110 — Fall 2019
Prelim 2 is on it's way!
2

 Check Canvas to see what time you have been


assigned to take the exam (5:30 or 7:30)
 If you can't make your assigned time, fill out the
Prelim 2 conflict survey by next Thursday
 Locations TBA
JavaHyperText Topics
3

“Graphs”, topics:
 4: DAGs, topological sort

 5: Planarity

 6: Graph coloring
4 Sorting
CS core course prerequisites (simplified)

2110 3410 4410

1110 3110

2800 4820

Problem: find an order in which you can


take courses without violating prerequisites

e.g. 1110, 2110, 2800, 3110, 3410, 4410, 4820


Topological order
6

A topological order of directed graph G is an


ordering of its vertices as v1, v2, …, vn, such that for
every edge (vi, vj), it holds that i < j.

Intuition: line up the vertices with all edges pointing left


to right.

3410 4410
2110
1110 3110 4820
2800

Other applications: dependencies between lines of code (data


flow graphs, control flow graphs), task scheduling, etc.
Cycles
7

 A directed graph can be topologically ordered if


and only if it has no cycles
 A cycle is a path v0, v1, ..., vp such that v0 = vp
 A graph is acyclic if it has no cycles
 A directed acyclic graph is a DAG
A A

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

 Deleting a vertex with indegree zero would not


remove any cycles
 Keep deleting such vertices and see whether graph
“disappears”
And the order in which we removed vertices was a topological order!
Algorithm: topological sort
9

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

How many colors are needed to ensure adjacent states


have different colors?
Graph coloring
12

Coloring: assignment of color to each vertex.


Adjacent vertices must have different colors.

B D

A F
E

How many colors needed?


Uses of graph coloring
13

And more! http://ijcit.org/ijcit_papers/vol3no2/IJCIT-130101.pdf


How to color a graph
14

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

return smallest c such that used[c] == 0;


}
How to color a graph
15

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() { Total time: O(E)


for each vertex v in graph:
c= find_color(neighbors of v);
color v with c;
} Time: O(# neighbors of v)
int find_color(vs) {
int[] used= new int[vs.length() + 1];
for each vertex v in vs:
Time:
if color(v) <= vs.length(): O(vs.length())
used[color(v)]++;
}
return smallest c such that used[c] == 0;
}
Analysis
17

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

Best coloring Worst coloring

Vertices labeled in order of processing


Only 2 colors needed for this special kind of graph…
Source: https://en.wikipedia.org/wiki/Grundy_number#/media/File:Greedy_colourings.svg
Bipartite graphs
19

Bipartite: vertices can be


A
partitioned into two sets such that
1
no edge connects two vertices in
B
the same set
2
C
Matching problems: 3
 Med students & hospital D
residencies Fact: G is bipartite
 TAs to discussion sections iff G is 2-colorable
 Football players to teams
Four Color Theorem
20

Every “map-like” graph is 4-colorable


[Appel & Haken, 1976]
Four Color Theorem
Proof required checking that 1,936 special graphs had
a certain property
 Appel & Haken used a computer program to check the
1,936 graphs
 Does that count as a proof?
 Gries looked at their computer program and found an
error; it could be fixed

In 2008 entire proof formalized in Coq proof


assistant [Gonthier & Werner]: see CS 4160
Four Color Theorem
22

Every “map-like” graph is 4-colorable


[Appel & Haken, 1976]

…“map-like”?
= planar
23 Planar Graphs
Planarity
24

A graph is planar if it can be drawn in the plane


without any edges crossing

B D

A F
E

Discuss: Is this graph planar?


Planarity
25

A graph is planar if it can be drawn in the plane


without any edges crossing

B D

A F
E

Discuss: Is this graph planar?


Planarity
26

A graph is planar if it can be drawn in the plane


without any edges crossing

B D

A F
E

Discuss: Is this graph planar?


YES!
Detecting Planarity
27

Kuratowski's Theorem:

K5
K3,3

A graph is planar if and only if it does not contain a


copy of K5 or K3,3 (possibly with other nodes along
the edges shown).
John Hopcroft & Robert Tarjan
28

 Turing Award in 1986 “for fundamental


achievements in the design and analysis of
algorithms and data structures”

 One of their fundamental achievements was a O(V)


algorithm for determining whether a graph is
planar.
David Gries & Jinyun Xue
29

Tech Report, 1988


Abstract: We give a rigorous, yet, we hope, readable,
presentation of the Hopcroft-Tarjan linear algorithm for
testing the planarity of a graph, using more modern
principles and techniques for developing and presenting
algorithms that have been developed in the past 10-12
years (their algorithm appeared in the early 1970's).
Our algorithm not only tests planarity but also
constructs a planar embedding, and in a fairly
straightforward manner. The paper concludes with a
short discussion of the advantages of our approach.

You might also like