09 Graphs (SF)
09 Graphs (SF)
Graphs
LEONHARD EULER
(1707–1783)
Multigraph Model
Introduction
Web Graph
Google PageRank
V = vertices = {a, b, c, d}
E = edges = {{a,b}, {a,c}}
A Call Graph
9.2 Graph Terminology and
Special Types of Graphs
Basic Terminology
Some Special Simple Graphs
Bipartite Graphs
Some Applications of Special Types of Graphs
New Graphs From Old
Basic Terminology - Undirected graphs
degree= 6
Theorem 2:
An undirected graph has an even number of vertices of odd
degree.
THE HANDSHAKING THEOREM
(for undirected graphs)
Ex2: How many edges does a graph have if its degree sequence
is 5, 5, 4, 3, 2, 1, 0?
Draw a such graph.
Solution:
deg v 5 5 4 3 2 1 0 20 2 E
vV
E 10
Basic Terminology - Directed graphs
In an directed graph G = (V; E), if (u; v) is an edge of G then:
• u is said to be adjacent to v and v is said to be adjacent from u,
• u is called initial vertex of (u; v)
• v is called terminal or end vertex of (u; v)
• the initial vertex and terminal vertex of a loop are the same.
Ex:
Basic Terminology - Directed graphs
The degree of a vertex v:
• deg v in-degree of a vertex v, is the number of edges with v as
K20
2
VK n n, EK n Cn
Special simple graphs
Cycles Cn , n ≥ 3: consists of n vertices v1 , v2 , , vn and edges
v1, v2 ,v2 , v3 , ,vn1, vn , and vn , v1. The cycles C3 , C4 , C5 , and C6
are displayed in Figure 4.
C8
VCn n, ECn n
Special simple graphs
Wheels: We obtain a wheel Wn when we add an additional vertex
to a cycle Cn , for n ≥ 3, and connect this new vertex to each of the
n vertices in Cn , by new edges.
Cycles Cn
C5 C6 C8
VWn n 1, EWn n n 2n
Special simple graphs
An n-dimensional hypercube, or n-cube, denoted by Qn , is a
n
graph that has vertices representing the 2 bit strings of length n.
Two vertices are adjacent if and only if the bit strings that they
represent differ in exactly one bit position.
Ex:
Study graph C6 (cycle)
V a, b, c, d , e, f
V1 a, e, c
V2 b, f , d
C6 is bipartite
Bipartite graphs
bipartite
Non- bipartite
Bipartite graphs
Theorem 4:
A simple graph is bipartite if and only if it is possible to assign
one of two different colors to each vertex of the graph so that no
two adjacent vertices are assigned the same color.
a b
• Let color the vertices
Ex: using 2 different colors
C6 is bipartite
• Two adjacent vertices
f c V1 = {a, c, e}
V2 = {b, d, f}
must have different colors
(e.g., red and black)
e d
a
c
d bipartite
b
V1 = {a, e, f}
V2 = {b, c, d}
f
e
Example
Ex: Is the graph G bipartite?
? G is not bipartite
G
A complete bipartite graph K m ,n is a graph that has:
- its vertex set partitioned into two subsets of m and n
vertices,
- an edge between two vertices if and only if one vertex is in
the first subset and the other vertex is in the second subset.
m=3
n=5
K3,5 VK m ,n m n, EK m ,n m.n
Some Complete Bipartite Graphs K m,n
Some Applications of
Special Types of Graphs
New Graphs From Old
New Graphs From Old
DEFINITION:
A subgraph of a graph G = (V, E) is a graph H = (W, F), where
W ⊆ V and F ⊆ E. A subgraph H of G is a proper subgraph of
G if H = G.
New Graphs From Old
DEFINITION:
The union of two simple graphs G1 V1 , E1 and G2 V2 , E2
is the simple graph with vertex set V1 V2 and edge set E1 E2.
The union of G1 and G2 is denoted by G1 G2 .
9.3 Representing Graphs and
Graph Isomorphism
Representing Graphs:
- Adjacency list,
- Adjacent Matrices,
- Incidence Matrices
Isomorphism of Graphs
Representing Graphs
Using Adjacency list: For each vertex u in the graph, there is
a list of vertex v which there is an edge between u and v.
Ex1:
Representing Graphs
Ex2:
Adjacency Matrices
Adjacency matrix.
A = [aij],
where aij = the number of
edges that are associated to {vi, vj}
Ex1:
Adjacency Matrices
Ex2:
Ex3:
For phrase matrices: it is usually preferable to use adjacency lists rather than an
adjacency matrix to represent the graph.
For dense matrices: using an adjacency matrix to represent the graph is usually
preferable over using adjacency lists.
Incidence Matrices
1, where edge e j is incident with vi ,
M nm mij , mij
0, otherwise.
Ex1:
the first column has two 1s. This means the edge e1 is incident with v1 and v4.
Incidence Matrices
Ex2:
the first column has only one 1. This means the edge e1 is a loop.
Representing Graphs
Adjacency matrix
Incidence matrix
edge 1 edge 2 … edge m
Vertex 1
Vertex 2
…
Vertex n
Isomorphism
THE SAME?
Isomorphism
THE SAME ?
Isomorphism
Definition: G1 V1 , E1 and G2 V2 , E2
G1 and G2 are isomorphic if f : V1 V2
oone-to-one and onto
o a, b are adjacent in G if and only if f a and f b
1
are adjacent in G2 , a, b V1.
Such a function f is called an isomorphism.
Ex1:
a b a b ab ac
a a
b c ac ad
c d cd d b
d b bd cb
c d c d
G1 G2
Two graphs are called isomorphic
Isomorphism
Ex2: a
b
a
e b
c
e
d d c
YES
Isomorphism
Ex3:
G H
Circuits
A circuit is a path of length greater than zero that starts
and ends at the same vertex (u=v).
Ex: c, b, e, a, d, c is circuit.
Simple paths/circuits
A path/circuit is simple if it does not contain the same edge
more than once.
Path
Ex: Determine the following ordered set of vertices whether it
is a path and if it is a path, what is it’s length?, is it a circuit?, is it
simple?
H is a disconnected with
G is connected 3 connected components
Connectedness in Undirected Graphs
o Connected = there is a path between every pair of distinct
vertices of the graph.
o Not connected = disconnected.
Remove b
Remove c
Remove b, c, e
Remove e
Connectedness in Undirected Graphs
Cut edge (bridge): It’s removal will produce subgraphs which are more
connected components than in the original graph
Connectedness in Undirected Graphs
A directed graph is strongly connected if there is a path from
a to b and from b to a whenever a and b are vertices in the graph.
A directed graph is weakly connected if there is a path
between every two vertices in the underlying undirected graph.
G H
G: strongly connected
weakly connected H: weakly connected
connected
Connectedness in Undirected Graphs
STRONG CONNECTED COMPONENTS:
The subgraphs of a directed graph that are strongly connected but
not contained in larger strongly connected subgraphs.
Ex:
H: weakly connected
Result = 8
9.5 Euler and Hamilton paths
Multigraph Model
Ex:
Ex: 3
NOT OK
2 2
2
4
2
OK
How to construct an Euler circuit?
procedure Euler (G: connected, every
a d
vertex has even degree)
Ex:
Ex:
There are
NO known simple
necessary and sufficient criteria
for the existence of
Hamilton circuits
Hamilton circuits – sufficient conditions
Dirac’s theorem.
Ore’s theorem.
G is a graph:
? 3 3
2
•simple
•n ( 3) vertices
•u, v, non-adjacent
deg(u) + deg(v) n
G has a Hamilton circuit
9.6 Shortest Path Problems
Introduction
A Shortest Path Algorithm
The Traveling Salesman Problem
Introduction
weight
Ex1:
Ex2:
Shortest Path Problems
Ex1:
What is a shortest path in air distance between Boston and Los Angeles?
Ex2:
8
0 a 1 2
a , c , b, d
z 14
13 a, c, b, d , e
3
2
c e
2 aa,,cc,b, d
10
a 12
10
3
0 a z
6
1
2
d e
3
2 5
S aa,,bb,,dd,,ee, z
S
S SS a,ab,a,dd
N SS cc,z
N
NNN
S S Sc,be,be,d
The length of the shortest path from a to z is 6.
The shortest path from a to z: a, d, e, z.
Dijkstra’s algorithm
O(n2)
time complexity
The Traveling Salesman Problem
Salesman starts in one city (ex. Detroit). He wants to visit n cities exactly
once and return to his starting point (Detroit). In which order should he
visit theses cities to travel the minimum total distance ?
The Traveling Salesman Problem
- The problem is equivalent to asking for a Hamilton circuit with minimum total weight.
- How many way do we have to examine to solve the problem if there are n vertex in the
graph? Exhaustive search technique
(n 1) n 2 n 2 3.2.1 n 1! O((n-1)!) complexity
2 2
Approximation algorithm
Summary
9.1- Graphs and Graph Models
9.2- Graph Terminology and Special Types of Graphs
9.3- Representing Graphs and Graph Isomorphism
9.4- Connectivity
9.5- Euler and Hamilton Paths
9.6- Shortest Path Problems
Thanks