11/18/2009
Introduction to Graphs
22c:19
Seven Bridges of Königsberg
Is it possible to walk with a route that crosses each bridge exactly once?
Seven Bridges of Königsberg
Forget unimportant details.
Forget even more.
1
11/18/2009
A Graph
A vertex
(or a node,
or a point)
An edge
(or a line)
d
e6
e7
e5
e4
a
c
e3
e2
e1
b
So, what is the “Seven Bridges of Königsberg” problem now?
To find a walk that visits each edge exactly once.
Euler’s Solution
Question: Is it possible to find a walk that visits each edge exactly once.
e6
d
e7
e5
e4
a
v
c
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.
Euler’s Solution
Question: Is it possible to find a walk that visits each edge exactly once.
d
e6
e4
a
So, at most two vertices can
have odd number of edges.
e7
e5
c
In this graph, every vertex has
only an odd number of edges,
e3
e2
e1
and so there is no walk which
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
Euler’s Solution
So Euler showed that the “Seven Bridges of Königsberg” 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?
Euler’s Solution
So Euler showed that the “Seven Bridges of Königsberg” 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?
NO!
Euler’s Solution
So Euler showed that the “Seven Bridges of Königsberg” 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”
and there are at most two vertices with odd number of edges?
YES!
3
11/18/2009
Euler’s Solution
So Euler showed that the “Seven Bridges of Königsberg” is unsolvable.
When is it possible to have a walk that visits every edge exactly once?
Euler path
Euler’s 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:21
22c:31
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
Marking
Tutorials
Darek
Solutions
Tom
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
Graph
Directed 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
a
An edge uv is incident on
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
N( ) = {d
{d,f},
f} N(d) = {a,b,c,f},
{ b f} N(e)
N( ) = {b,c}.
{b }
e.g. N(a)
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)?
Is there a graph with degree sequence (3,3,3,3)?
Is there a graph with degree sequence (2,2,1)?
YES.
YES.
NO.
2
2
1
Is there a graph with degree sequence (2,2,2,2,1)?
NO.
What’s wrong with these sequences?
Where to go?
6
11/18/2009
Handshaking Lemma
For any graph, sum of degrees = twice # edges
Lemma.
Corollary.
2|E | =
∑
v ∈V
deg(v )
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.
Proof.
2|E | =
∑
v ∈V
deg(v )
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
122
257
145
99
67
306
99
67
306
145
Same graph (different labels)
257
122 145
306
67
Albert
Grant Sharat
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.
Dog
Hay
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.
Dog
Hay
Pig
Corn
Cow
Cat
Edge preserved?
YES!
Beef
Tuna
If there is an edge in the original graph, there is an edge after the mapping.
Are These Isomorphic?
A bijection.
Dog
Hay
Pig
Corn
Cow
Cat
Non-Edge preserved?
Beef
Tuna
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
p p
Find some isomorphic-preserving
which is satisfied in one graph but not the other.
Non-isomorphic
all degree 3
degree 2
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
10
4
8
16
66
E17
E25
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.
a
w
b
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 doesn’t cross itself (i.e., no
repeated vertices)
w
also: w ···v ···w
cycle: v ···w ···v
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.
c
v
u
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
v
u
P1
P2
This is a cycle
12
11/18/2009
Summary
x
a
y
simple path: no vertex can be repeated
b
example path: a-b-c-d-e
path: no edge can be repeated
p trail: a-b-c-d-e-b-d
example
walk: no restriction
example walk: a-b-d-a-b-c
e
d
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
b
a
a
e
e
b
K5
d
c
Disconnected graph
with two components
d
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 (edgeweighted) and/or the vertices (vertex-weighted)
b
10
5
d
c
8
-3
2
e
a
6
f
39
13
11/18/2009
Digraph definitions
• for digraphs only…
Directed graph
a
• Every edge has a head (starting point) and a
b
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
d
(nodes at the tail of any path).
x
• parent: direct ancestor (one hop)
y
w
• child: direct descendent (one hop)
• A descendent vertex is reachable from any of
v
u
its ancestors vertices
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
c
b
2
8
a
4
a
10
d
a
b
c
d
b
c
8
a c (8), d (4)
b
c b (6)
d c (2), b (10)
d
4
6
10 2
a
b
c
d
adjacency
list
adjacency
matrix
t
6
2
8
t
3
4
t
t
2 10
5
4
t
incidence
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
b
e
d
d
c
G=(V,E)
d
c
G’=({a,c,d},{{c,d}})
c
Induced subgraph of
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
b
G
G
e
b
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
4
3
Maximal cliques: {1,2,3},{1,3,4}
Vertex cover: {1,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
d
e
g
h
c
a
f
b
1
2
3
5
i
3
3
4
c
e
4
d
2
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?
e
b
d
a
c
f
h
g
cut-sets: {(a,b),(a,c)},
{(b,d),(c,d)},{(d,f)},...
1
2
4
3
47
Euler Graphs
Euler’s 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?
Euler’s 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
2
4
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
2
4
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
2
4
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
3
5
2
12
7
4
9
13
16
10
14
11
8
Let’s say we’ve 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
3
15
2
4
14
10
7
11
5
8
12
9
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
© Copyright 2026 Paperzz