11/18/2009
Introduction to Graphs
22c:19
Seven Bridges of Knigsberg
Is it possible to walk with a route that crosses each bridge exactly once?
Seven Bridges of Knigsberg
Forget unimportant details. Forget even more.
1
11/18/2009
A Graph
An edge
A vertex e6 d (or a line)
(or a node,
or a point) e7
e5
e4
c
a
e3
e2
e1
b
So, what is the Seven Bridges of Knigsberg problem now?
To find a walk that visits each edge exactly once.
Eulers Solution
Question: Is it possible to find a walk that visits each edge exactly once.
e6 d
e7
e5
e4 v
c
a
e3
e2
e1
b
Suppose there is such a walk, there is a starting point and an endpoint point.
For every intermediate point v, there must be the same number of
incoming and outgoing edges, and so v must have an even number of edges.
Eulers Solution
Question: Is it possible to find a walk that visits each edge exactly once.
e6 d
So, at most two vertices can
e7 have odd number of edges.
e5
e4
c In this graph, every vertex has
a only an odd number of edges,
e3
e2 and so there is no walk which
e1
visits each edge exactly one.
b
Suppose there is such a walk, there is a starting point and an endpoint point.
For every intermediate point v, there must be the same number of
incoming and outgoing edges, and so v must have an even number of edges.
2
11/18/2009
Eulers Solution
So Euler showed that the Seven Bridges of Knigsberg is unsolvable.
When is it possible to have a walk that visits every edge exactly once?
Is it always possible to find such a walk if there is
at most two vertices with odd number of edges?
Eulers Solution
So Euler showed that the Seven Bridges of Knigsberg is unsolvable.
When is it possible to have a walk that visits every edge exactly once?
Is it always possible to find such a walk if there is
NO!
at most two vertices with odd number of edges?
Eulers Solution
So Euler showed that the Seven Bridges of Knigsberg is unsolvable.
When is it possible to have a walk that visits every edge exactly once?
Is it always possible to find such a walk if the graph is connected
YES!
and there are at most two vertices with odd number of edges?
3
11/18/2009
Eulers Solution
So Euler showed that the Seven Bridges of Knigsberg is unsolvable.
When is it possible to have a walk that visits every edge exactly once?
Euler path
Eulers theorem: A graph has an Euler path if and only if it is
connected and has at most two vertices with an odd number of edges.
This theorem was proved in 1736,
and was regarded as the starting point of graph theory.
Application 1
Graph is very useful in representing relations.
22c:19
22c:31
22c:21 22c:22
Each course is a vertex.
There is an edge between two courses if some student is taking both.
How can we schedule the exams in a minimum number of days
so that no students would have conflicts?
This is a graph coloring problem.
Application 2
Each computer is a vertex.
There is an edge between two
computers if there is a direct
connection
connection.
How to find a shortest route between two computers?
How to find a cheapest network to connect all computers?
How to send the maximum amount of information between two computers?
These are all graph problems (shortest path, minimum spanning tree, maximum flow).
4
11/18/2009
Application 3
Each junction is a vertex.
Each edge is a road.
How to make every road an one-way-road so that
everywhere can still be reached from everywhere?
This is a graph orientation problem.
Application 4
Isaac Jerry Darek Tom
Marking Tutorials Solutions Newsgroup
Each person is a vertex above.
Each task is a vertex below.
There is an edge between a person and a task
if that person is willing to take up that task.
How to assign people to tasks so that all tasks can be finished?
This is a graph matching problem.
Types of Graphs
Will see later
Simple Directed Graph
Graph
Multi-Graph Euler path
problem
Most of the
problems in
this course.
5
11/18/2009
Simple Graphs
a
b
A graph G=(V,E) consists of:
A set of vertices, V
f
A set of undirected edges, E
c
V(G) = {a,b,c,d,e,f} e
d
E(G) = {ad,af,bd,be,cd,ce,df}
G
Two vertices a,b are adjacent (neighbours) if the edge ab is present.
Vertex Degrees
An edge uv is incident on a
b
the vertex u and the vertex v.
f
The neighbour set N(v) of a vertex v
is the set of vertices adjacent to it. c
e.g. N(a)
N( ) = {d
{d,f},
f} N(d) = {a,b,c,f},
{ b f} N(e)N( ) = {b,c}.
{b } e
d
degree of a vertex = # of incident edges
e.g. deg(d) = 4, deg(a)=deg(b)=deg(c)=deg(e)=deg(f)=2.
the degree of a vertex v = the number of neighbours of v?
For multigraphs, NO. For simple graphs, YES.
Degree Sequence
Is there a graph with degree sequence (2,2,2)? YES.
Is there a graph with degree sequence (3,3,3,3)? YES.
Is there a graph with degree sequence (2,2,1)? NO. 2
2 1
Is there a graph with degree sequence (2,2,2,2,1)?
Where to go?
NO. Whats wrong with these sequences?
6
11/18/2009
Handshaking Lemma
For any graph, sum of degrees = twice # edges
Lemma.
2|E | = deg(v )
v V
Corollary. 1. Sum of degree is an even number.
2. Number of odd degree vertices is even.
Examples. 2+2+1 = odd, so impossible.
2+2+2+2+1 = odd, so impossible.
Handshaking Lemma
Lemma.
2|E | = deg(v )
v V
Proof. Each edge contributes 2 to the sum on the right. Q.E.D.
Question. Given a degree sequence, if the sum of degree is even,
is it true that there is a graph with such a degree sequence?
For simple graphs, NO, consider the degree sequence (3,3,3,1).
For multigraphs (with self loops), YES! (easy by induction)
Same Graphs?
Same graph (different drawings)
257 122
257 122 145
99
306 67
99 67 306 145
Same graph (different labels)
257 122 145 Albert Grant Sharat
306 67 Christos
Sonya
99 Jessica
7
11/18/2009
Review of Basic definitions
Undirected graph Directed graph
loop loop
G=(V,E)
isolated vertex
adjacent
multiple
edges
incidence: an edge (directed or undirected) is incident to a vertex
that is one of its end points.
degree of a vertex: number of edges incident to it
Nodes of a digraph can also be said to have an indegree and an outdegree
adjacency: two vertices connected by an edge are adjacent
22
Graph Isomorphism
All that matters is the connections.
Graphs with the same connections are isomorphic.
Informally, two graphs are isomorphic if they are the same
after renaming of vertices
G1 isomorphic to G2 means there is an edge-preserving vertex matching.
relation preserving renaming function
Graph isomorphism has applications like checking fingerprint, testing molecules
Are These Isomorphic?
A bijection.
Hay
Dog Pig
Corn
Cow Cat Beef Tuna
f (Dog) = Beef f (Cow) = Hay
f (Cat) = Tuna f (Pig) = Corn
8
11/18/2009
Are These Isomorphic?
A bijection.
Hay
Dog Pig
Corn
Cow Cat Beef Tuna
Edge preserved? YES!
If there is an edge in the original graph, there is an edge after the mapping.
Are These Isomorphic?
A bijection.
Hay
Dog Pig
Corn
Cow Cat Beef Tuna
Non-Edge preserved? YES!
If there is no edge in the original graph, there is no edge after the mapping.
Graph Isomorphism
G1 isomorphic to G2 means there is an edge-preserving vertex matching.
bijection f: V1 V2
(u,v) in E1 iff (f (u),f (v)) in E2
uv is an edge in G1 f(u)f(v) is an edge in G2
If G1 and G2 are isomorphic, do they have the same number of vertices? YES
If G1 and G2 are isomorphic, do they have the same number of edges? YES
If G1 and G2 are isomorphic, do they have the same degree sequence? YES
If G1 and G2 have the same degree sequence, are they isomorphic? NO
9
11/18/2009
Exercise
How to show two graphs are isomorphic?
Find a mapping and show that it is edge-preserving.
How to show two graphs are non-isomorphic?
p p g properties
Find some isomorphic-preserving p p
which is satisfied in one graph but not the other.
Non-isomorphic
degree 2 all degree 3
Exercise
Which is isomorphic to G1?
Only G1 and G2:
1-1, 2-2, 3-3, 4-8, 5-9,
6-10, 7-4, 8-5, 9-6, 10-7
Testing graph isomorphism is not easy
No known general method to test graph ismorphism
much more efficient than checking all possibilities.
Walks, Paths, and Simple Paths
Walk: sequence of adjacent vertices
( )
Path: sequence of adjacent vertices
without repeated edges
Simple Path: sequence of adjacent
vertices without repeated vertices
10
11/18/2009
Simple Paths
Simple Path: all vertices different ( )
Connectedness
Vertices v, w are connected if and only if
there is a path starting at v and ending at w.
A graph is connected iff every pair of vertices are connected.
Every graph consists of separate connected pieces called connected components
13 12 26 16 66
E17
E25
10 4 8
3 connected components
So a graph is connected if and only if it has only 1 connected component.
Cycles
A cycle (circuit) is a closed path that begins and ends with same vertex.
cycle: v b w w a v
also: a v b w w a
11
11/18/2009
Simple Cycles
A simple cycle is a cycle that doesnt cross itself (i.e., no
repeated vertices)
cycle: v w v also: w v w
In a simple cycle, every vertex appears at most once.
Shortest Paths
A path between u and v is a shortest path
if among all u-v paths it uses the minimum number of edges.
Is a shortest path between two vertices always simple?
Idea: remove the cycle will make the path shorter.
u v
Shortest Paths
Lemma. A shortest path between two vertices is simple.
Proof (by contradiction):
1. Suppose there is a non-simple shortest path P=(u,,v)
2. Let c be the first vertex that is visited twice.
3. Then P=(u,P1,c,P2,c,P3,v).
4. But P=(u,P1,c,P3,v) is shorter, a contradiction.
c
P3
u v
P1 P2
This is a cycle
12
11/18/2009
Summary
x y a
simple path: no vertex can be repeated e
example path: a-b-c-d-e b
path: no edge can be repeated
p trail: a-b-c-d-e-b-d
example
walk: no restriction d
example walk: a-b-d-a-b-c
c
closed: if starting vertex is also ending vertex
length: number of edges in the simple path, path, or walk
cycle or circuit: a closed walk (ex: a-b-c-d-b-e-d-a)
simple cycle: closed simple path (ex: a-b-c-d-a)
37
Types of graphs
simple graph: an undirected graph with no loops or multiple edges between
the same two vertices
multi-graph: any graph that is not simple
connected graph: all vertex pairs are joined by a path
disconnected graph: at least one vertex pairs is not joined by a path
complete graph: all vertex pairs are adjacent
Kn: the completely connected graph with n vertices
Simple graph a
a e
b
e
b
K5
d
c d
Disconnected graph
with two components c
38
Types of graphs
acyclic graph (forest): a graph with no cycles
tree: a connected, acyclic graph
rooted tree: a tree with a root or distinguished vertex
leaves: the terminal nodes of a rooted tree
directed acyclic graph (dag): a digraph with no cycles
weighted graph: any graph with weights associated with the edges (edge-
weighted) and/or the vertices (vertex-weighted)
b 10 a
5 c
d 8
2 -3
e f
6
39
13
11/18/2009
Digraph definitions
for digraphs only Directed graph
Every edge has a head (starting point) and a b a
tail (ending point)
Walks, trails, and paths can only use edges in
the appropriate direction
In a DAG, every path connects an c
predecessor/ancestor (the vertex
erte at the head
of the path) to its successor/descendents
(nodes at the tail of any path). d
x
parent: direct ancestor (one hop)
w y
child: direct descendent (one hop)
A descendent vertex is reachable from any of v
its ancestors vertices u
z
40
Computer representation
undirected graphs: usually represented as digraphs with two
directed edges per actual undirected edge.
adjacency matrix: a |V| x |V| array where each cell i,j contains
the weight of the edge between vi and vj (or 0 for no edge)
adjacency list: a |V| array where each cell i contains a list of all
vertices adjacent to vi
incidence matrix: a |V| by |E| array where each cell i,j
i j contains
a weight (or a defined constant HEAD for unweighted graphs)
if the vertex i is the head of edge j or a constant TAIL if vertex I
is the tail of edge j
6 1 2 3 4 5
c b a
a b c
8
d
4
a c (8), d (4)
b
a
b t
8
t
4
b c b (6) c 6 t t
2 c 6 d c (2), b (10) d 2 10 t
8 10 d 10 2
4 adjacency incidence
a d adjacency
list matrix
matrix
41
Subgraphs
G=(V,E) is a subgraph of G=(V,E) if V V and E E.
induced subgraph: a subgraph that contains all possible edges
in E that have end points of the vertices of the selected V
a a
b e
e
b
d
d d c
c c Induced subgraph of
G=(V,E) G=({a,c,d},{{c,d}}) G with V = {b,c,d,e}
42
14
11/18/2009
Complement of a graph
The complement of a graph G (V,E) is a graph with the same
vertex set, but with vertices adjacent only if they were not
adjacent in G(V,E)
a a
e e
b b
G G
d d
c c
43
Famous problems: Maximal clique
clique: a complete subgraph
maximal clique: a clique not contained in any other clique; the
largest complete subgraph in the graph
vertex cover: a subset of vertices such that each edge in E has
at least one end-point in the subset
1 2 Maximal cliques: {1,2,3},{1,3,4}
Vertex cover: {1,3}
4 3
44
Famous problems: Hamilton & TSP
Hamiltonian path: a path through a graph which contains
every vertex exactly once
Finding a Hamiltonian path is another NP-complete problem
Traveling Salesmen Problem (TSP): find a Hamiltonian path
off minimum
i i costt
a b c a 3
1 e
d e f b 3 2 4
3
d
g h 5 4
2
i c
45
15
11/18/2009
Famous problems: Bipartite graphs
Bipartite: any graph whose vertices can be partitioned into two
distinct sets so that every edge has one endpoint in each set.
How colorable is a bipartite graph?
Can you come up with an algorithm to determine if a graph is
bipartite or not?
K4,4
46
Famous problems: Minimal cut set
cut set: a subset of edges whose remove causes the number of
graph components to increase
vertex separation set: a subset of vertices whose removal
causes the number of graph components to increase
How would you determine the minimal cut set or vertex
separation set?
b e
cut-sets: {(a,b),(a,c)},
d f {(b,d),(c,d)},{(d,f)},...
a h
c g 1 2
4 3
47
Euler Graphs
Eulers theorem: A graph has an Euler path if and only if it is
connected and has at most two vertices with an odd number of edges.
Can a graph has only 1 Odd degree vertices.
odd degree vertex?
Eulers theorem: A graph has an Euler path if and only if
it has zero or two vertices with odd degrees.
Proof by induction.
16
11/18/2009
Proof Idea
Assume every vertex is of even degree.
Pick any vertex, go to unvisited edges as long as possible.
1
6
3 5
Get stuck. Now what?
Proof Idea
Assume every vertex is of even degree.
Pick any vertex, go to unvisited edges as long as possible.
1
6
3 5
Idea: Remove the edges already visited.
Proof Idea
Assume every vertex is of even degree.
Again, pick any vertex, go to unvisited edges as long as possible.
1
6
3 5
The remaining graph is still Euler (why?)
17
11/18/2009
Proof Idea
Assume every vertex is of even degree.
Again, pick any vertex, go to unvisited edges as long as possible.
1 15
6 12 9
3 5 13 10
16
2 14 11
7
4 8
Lets say weve found an Euler path in the remaining graph.
Now what?
Proof Idea
Assume every vertex is of even degree.
Idea: Combine the two Euler paths.
Common vertex v
1 13
16 10 7
3 15 11 8
14
2 12 9
5
4 6
1. Go along the first Euler path until reaching v,
2. follow the second Euler path visiting all the edges until coming back v,
3. then follow the remaining first Euler path.
Complication
What if we start and then get stuck in the blue vertex?
18
11/18/2009
Complication
What if we start and then get stuck in the blue vertex?
Then the remaining graph is disconnected into more components.
The same idea applies.
19