Вы находитесь на странице: 1из 81

Unit IV Graph Theory (07 Hours)

Graph Terminology and Special Types of Graphs, Representing Graphs and Graph
Isomorphism, Connectivity, Euler and Hamilton Paths, the handshaking lemma, Single
source shortest path- Dijkstra's Algorithm, Planar Graphs, Graph Coloring.

#Exemplar/CaseStudies Three utility problem, Web Graph, Google map


Graph Theory
What is a graph?

 A set of node (vertex) and a set of edge that relate the


nodes to each other
 The set of edges describes relationships among the
vertices
Formal definition of graphs
A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
E is a multiset, in other words, its elements can
occur more than once so that every element has a
multiplicity.
Often, label the vertices with letters (for example:
a, b, c, . . . or v1, v2, . . .) or numbers 1, 2, . . .
Similarly, label the edges
with letters (for example: a,
b, c, . . . or e1, e2, . . .) or
numbers 1, 2, . . . for
simplicity as shown below.
We will label graphs with
letters, for example:
G = (V, E).

The minimum degree of the vertices in a graph G is


denoted δ(G) (= 0 if there is an isolated vertex in G).
 Similarly, we write ∆(G) = 5 as the maximum degree
of vertices in graph G.
Graph Terminologies
Following terminologies are associated with graph:
1. The two vertices u and v are end vertices of the edge (u,
v) , e1.
2. Edges that have the same end vertices are parallel edge.
3. An edge of the form (v, v) is a loop.
4. A graph is simple if it has no parallel edges or loops.
5. A graph with no edges (i.e. E is empty) is empty graph.
6. A graph with no vertices (i.e. V and E are empty) is a null
graph.
7. A graph with only one vertex is trivial graph.
Graph Terminologies(Cont.)
8. Edges are adjacent if they share a common end vertex.
9. Two vertices u and v are adjacent if they are connected
by an edge, in other words, (u, v) is an edge.
10. Path: a sequence of vertices that connect two nodes in
a graph
11. Complete graph: a graph in which every vertex is
directly connected to every other vertex.
12. The degree of the vertex v, written as d(v), is the
number of edges with v as an end vertex. By convention,
we count a loop twice and parallel edges contribute
separately.
Graph Terminologies(Cont.)
13. A pendant vertex is a vertex whose degree is 1, An edge that has a
pendant vertex as an end vertex is a pendant edge.
14. An isolated vertex is a vertex whose degree is 0.

In graph above
v4 and v5 are end vertices of e5, e4 and e5 are parallel,
e3 is a loop, The graph is not simple, e1 and e2 are adjacent, v1 and v2 are
adjacent, The degree of v1 is 1 so it is a pendant vertex, e1 is a pendant
edge, The degree of v5 is 5, The degree of v4 is 2, The degree of v3 is 0 so it
is an isolated vertex.
Graph terminology (cont.)
What is the number of edges in a complete
directed graph with N vertices? 
N * (N-1)

2
O( N )
Graph terminology (cont.)
What is the number of edges in a complete
undirected graph with N vertices? 
N * (N-1) / 2
2
O( N )
Directed vs. undirected graphs

When the edges in a graph have no direction, the


graph is called undirected
Directed vs. undirected graphs (cont.)

When the edges in a graph have a direction, the


graph is called directed (or digraph)

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !!

E(Graph2) = {(1,3) (3,1) (5,9) (9,11)


(5,7)
Weighted graph
A graph in which each edge carries a value
Vertices (aka nodes)

618 3

2
1 2273 211
190

318
4
344 1987
2145 5

2462

Weights
6
Undirected Edges
Graph Representation
Two popular computer representations of a graph. Both
represent the vertex set and the edge set, but in different
ways.
1. Adjacency Matrix: Most useful when information about
edges is more desirable than information about vertices.
Use a 2D matrix to represent the graph

2. Adjacency List: Most useful when information about the


vertices is more desirable than information about the
edges.
Use a 1D array of linked lists
Representation- Adjacency Matrix
 There is an N x N matrix, where |V| = N , the Adjacenct Matrix
(NxN) A = [aij]

For undirected graph

1 if {vi, vj} is an edge of G


a ij  
0 otherwise

 For directed graph


1 if (vi, vj) is an edge of G
a ij  
0 otherwise

 This makes it easier to find subgraphs, and to reverse graphs if needed.


Representation- Adjacency Matrix
 Example: Undirected Graph G (V, E)

v u w
u
v 0 1 1

u 1 0 1
v w
w 1 1 0
Representation- Adjacency Matrix
 Example: directed Graph G (V, E)

v u w
u
v 0 1 0

u 0 0 1
v w
w 1 0 0
Graph implementation
Array-based implementation
A 1D array is used to represent the vertices
A 2D array (adjacency matrix) is used to represent
the edges
Array-based implementation
Adjacency Matrix Example

0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
8
1 0 0 1 1 0 0 0 1 0 1
2 9 2 0 1 0 0 1 0 0 0 1 0
1 3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
3 7
6 5 0 0 0 1 0 0 1 0 0 0
4 6 0 0 0 0 0 1 0 1 0 0
5
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
Representation- Adjacency List
Each node (vertex) has a list of which nodes (vertex) it is adjacent

Example: undirectd graph G (V, E)

u
node Adjacency List

u v,w

v w, u
v w
w u,v
Graph implementation (cont.)
Linked-list implementation
A 1D array is used to represent the vertices
A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list implementation
Adjacency List Example
0 8
0
1 2 3 7 9
8
2 1 4 8

2 9 3 1 4 5

1 4 2 3
5 3 6
3 7
6 5 7
6
4 7 1 6
5
8 0 2 9
9 1 8
Adjacency matrix vs. adjacency list representation
Adjacency matrix
Good for dense graphs --|E|~O(|V|2)
Memory requirements: O(|V| + |E| ) = O(|V|2 )
Connectivity between two vertices can be tested
quickly
Adjacency list
Good for sparse graphs -- |E|~O(|V|)
Memory requirements: O(|V| + |E|)=O(|V|)
Vertices adjacent to another vertex can be found
quickly
Paths and Circuits
 A walk in a graph is an alternating sequence of adjacent vertices

and edges
 A path is a walk that does not contain a repeated edge

 Simple path is a path that does not contain a repeated vertex

 A closed walk is a walk that starts and ends at the same vertex

 A circuit is a closed walk that does not contain a repeated edge

 A simple circuit is a circuit which does not have a repeated

vertex except for the first and last


Connectedness
 Two vertices of a graph are connected when there is a walk between two of

them.
 The graph is called connected when any pair of its vertices is connected

 If graph is connected, then any two vertices can be connected by a simple

path
 If two vertices are part of a circuit and one edge is removed from the circuit

then there still exists a path between these two vertices


 Any graph is a union of connected components
Planar Graph
In graph theory, a planar graph is a graph that can be embedded in
the plane, i.e., it can be drawn on the plane in such a way that its
edges intersect only at their endpoints. In other words, it can be
drawn in such a way that no edges cross each other.
Example graphs
Planar Nonplanar

Butterfly graph
Complete graph K5

Complete graph
Utility graph K3,3
K4
Simple graphs – special cases

Complete graph: Kn, is the simple graph that contains exactly one edge
between each pair of distinct vertices.

Representation Example: K1, K2, K3, K4

K1 K2 K3
K4
Simple graphs – special cases

Cycle: Cn, n ≥ 3 consists of n vertices v1, v2, v3 … vn and edges {v1,


v2}, {v2, v3}, {v3, v4} … {vn-1, vn}, {vn, v1}
Representation Example: C3, C4

C3 C4
Simple graphs – special cases

Wheels: Wn, n>=3obtained by adding additional vertex to Cn and

connecting all vertices to this new vertex by new edges.

Representation Example: W3, W4

W3 K4 W4
Simple graphs – special cases
N-cubes: Qn, vertices represented by 2n bit strings of length n. Two
vertices are adjacent if and only if the bit strings that they represent differ
by exactly one bit positions

Representation Example: Q1, Q2

10 11

0 1

00 01

Q1 Q2 Q3
Bipartite graphs
In a simple graph G, if V can be partitioned into two disjoint sets V1 and V2 such that every edge in the graph
connects a vertex in V1 and a vertex V2 (so that no edge in G connects either two vertices in V1 or two vertices in V2)
Application example: Representing Relations
Representation example: V1 = {v1, v2, v3} and V2 = {v4, v5, v6}

v4
v1

v5
v2

v6
v3

V V2
1
Complete Bipartite graphs
Km,n is the graph that has its vertex set portioned into two subsets of m and n
vertices, respectively There is 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.

Representation example: K2,3, K3,3 m*n

K2,3 K3,3
Subgraphs
A subgraph of a graph G = (V, E) is a graph H =(V’, E’) where V’ is a subset of V
and E’ is a subset of E
Application example: solving sub-problems within a graph
Representation example: V = {u, v, w}, E = ({u, v}, {v, w}, {w, u}}, H 1 ,
H2

u u u

v w v w v

G H1 H2
Subgraphs
G = G1 U G2 wherein E = E1 U E2 and V = V1 U V2, G, G1 and G2 are simple graphs of G

Representation example: V1 = {u, w}, E1 = {{u, w}}, V2 = {w, v},


E1 = {{w, v}}, V = {u, v ,w}, E = {{{u, w}, {{w, v}}

u
u

w v
w w v

G1 G2 G
Isomorphism of Graphs
Graphs G1 and G2 are said to be isomorphic if −
Their number of components (vertices and edges) are same.
Their edge connectivity is retained.
Number of vertices of a particular degree
Possession of a circuit of a particular length
Possession of Euler circuit, Hamiltonian circuit
An isomorphism
Graph G Graph H
between G and H
f(a) = 1f(b) = 6
f(c) = 8
f(d) = 3
f(g) = 5
f(h) = 2
f(i) = 4
f(j) = 7
Graph searching
Problem: find a path between two nodes of the
graph
Methods: Depth-First-Search (DFS) or Breadth-
First-Search (BFS)
Depth-First-Search (DFS)
What is the idea behind DFS?
Travel as far as you can down a path
Back up as little as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
DFS can be implemented efficiently using a stack
LIFO Last In First Out (Books)
Breadth-First-Search (BFS)
What is the idea behind BFS?
Look at all possible paths at the same depth before
you go at a deeper level
Back up as far as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
BFS can be implemented efficiently using a queue
FIFO
Single-source shortest-path problem
There are multiple paths from a source vertex to a
destination vertex
Shortest path: the path whose total weight (i.e., sum
of edge weights) is minimum.
• Examples of the algorithms that compute these
shortest path are Dijkstra and Bellman-Ford
algorithms as well as algorithms that find the
shortest path between all pairs of nodes, e.g. Floyd-
Marshall.
Single-source shortest-path problem
(cont.)
Common algorithms: Dijkstra's algorithm,
Bellman-Ford algorithm
BFS can be used to solve the shortest graph
problem when the graph is weightless or all the
weights are the same
(mark vertices before Enqueue)
Dijkstra’s Algorithm
Assume V1 is s and Dv is the 1
V2 V3
distance from node s to node v. 4
1 2 3
5
V6
If there is no edge connecting V1 V7
4 4
two nodes x and y → w(x,y) = 3 2
∞ V4 V5
6

D2=1 1 D3=∞ 1 D3=2


V2 V3 D2=1 V2 V3
4 1 3 4
1 2 3 2
5 5
V1 D7=∞ V7 V6 V1 D7=3 V7 V6

4 4 D6=∞ 4 4 D6=∞
3 3 2
2
V4 6 V5 V4 6 V5
D5=∞
D4=3 D4=3 D5=∞
V’ = {1} V’ = {1,2}
Contd…
1 1
D2=1 V2 V3 D3=2 D2=1 V2 V3 D3=2
1 2 3 4 1 2 3 4
5 5
V1 D7=3 V7 V6 V1 D7=3 V7 V6
4 4 D6=6 4 4 D6=6
3 2 3 2
V4 6 V5 D4=3 V4 6 V5 D =9
D4=3 D5=∞ 5

V’ = {1,2,3} V’ = {1,2,3,4}

1 1
D2=1 V2 V3 D3=2 D2=1 V2 V3 D3=2
1 2 3 4 1 2 3 4
5 5
V1 D7=3 V7 V6 V1 D7=3 V7 V6
4 4 D6=6 4 4 D6=6
3 2 3 2
V4 6 V5 V4 6 V5
D4=3 D5=7 D4=3 D5=7
V’ = {1,2,3,4,7} V’ = {1,2,3,4,7,6}
Contd…
1
D2=1 V2 V3 D3=2
The algorithm terminates
1 2 3 4
when all the nodes have
5
V1 D7=3 V7 V6 been processed and their
4 4 D6=6 shortest distance to node 1
3 2 has been computed
V4 6 V5
D4=3 D5=7
V’ = {1,2,3,4,7,6,5}
1
V2 V3
1 2 3 4
Note that the tree computed is
5
not a minimum weight V1 V7 V6
spanning tree. A MST for the 4 4
given graph is → 3 2
V4 6 V5
Bellman-Ford Algorithm
1 Until (Dvh = Dvh-1  v V ) or (h = |V |)
V2 V3
do
1 2 3 4 h = h + 1;
5 For v V do
V1 V7 V6
Dv = min{Du + w(u,v)} u V;
h+1 h

4 4
3 2 EndFor
EndUntil
V4 6 V5

h=1 h=2 h=3 h=4


1
D2 h 1 1 1 1 V2 V3

h 1 2 3 4
D3 ∞ 2 2 2
5
h V1 V7 V6
D4 3 3 3 3
4 4
h ∞ 9 7 7 3 2
D5
h V4 6 V5
D6 ∞ ∞ 6 6
h ∞ 3 3 3
D
Floyd-Warshall Algorithm
D= W
V2
2
For u = 1 to |V | do
2
2 For s = 1 to |V | do
8
6 For v = 1 to |V | do
V1 V3 Ds,v = min{Ds,v , Ds,u+ Wu,v}
4
1 EndFor
EndFor
1
4
3 EndFor
1 3
V4 V5
5

V1 V2 V3 V4 V5

 0 2 4  3 V1  0 2 4  3
 2 0 8  1 V2
 2 0 8  1
   
D0   6 2 0 4 3 V3 D1   6 2 0 4 3
   
 1   0 5  V4
 1 3 5 0 4
   1 0 V5    1 0
Floyd-Warshall Algorithm (Example)
 0 2 4  3  0 2 4 8 3
 2 0 6  1  2 0 6 10 1 
   
D2   4 2 0 4 3 D3   4 2 0 4 3
   
 1 3 5 0 4  1 3 5 0 4 
   1 0    1 0

0 2 4 8 3 0 2 4 4 3
2 2 1 
 0 6 10 1   0 6 2
D4  4 2 0 4 3 D5  4 2 0 4 3
   
1 3 5 0 4 1 3 5 0 4
2 4 6 1 0 2 4 6 1 0
Distributed Asynchronous Shortest Path
Algorithms
Each node computes the path with the shortest
weight to every network node
There is no centralized computation
As for the distributed MST algorithm described in
[Gallager, Humblet, and Spiral], control messaging
is required to distributed computation
Asynchronous means here that there is no
requirement of inter-node synchronization for the
computation performed at each node of for the
exchange of messages between nodes
Distributed Bellman-Ford Algorithm Example

Initial Ds,V
1 C
B A B C D E
s
7
A 0 7 ∞ ∞ 1
A 8 2 B 7 0 1 ∞ 8
C ∞ 1 0 2 ∞
1 D ∞ ∞ 2 0 2
E D E 1 8 ∞ 2 0
2

Ds,V
1
B C
s A B C D E
7
A 0 7 ∞ ∞ 1
A 8 2 B 7 0 1 ∞ 8
C ∞ 1 0 2 ∞
1
D ∞ ∞ 2 0 2
E D E 1 8 4 2 0
2
E receives D’s routes and updates its Ds,V
Distributed Bellman-Ford Algorithm Example

A receives B’s routes and updates its Ds,V


Ds,V
1 C
B A B C D E
s
7
A 0 7 8 ∞ 1
A 8 2 B 7 0 1 ∞ 8
C ∞ 1 0 2 ∞
1
D ∞ ∞ 2 0 2
E D E 1 8 4 2 0
2
A receives E’s routes and updates its Ds,V
Ds,V
1
B C
s A B C D E
7
A 0 7 5 3 1
A 8 2 B 7 0 1 ∞ 8
C ∞ 1 0 2 ∞
1
D ∞ ∞ 2 0 2
E D E 1 8 4 2 0
2
Distributed Bellman-Ford Algorithm Example

A’s routing table


1
B C
Destination Next Hop Distance
7
B E 6
A 8 2 C E 5
D E 3
1
E E 1
E D
2
1 E’s routing table
B C
Destinatio
7 Next Hop Distance
n
A 8 A A 1
2
B D 5
1 C D 4
E D D D 2
2
Edge Connectivity

Bridge    A bridge is a single edge whose removal disconnects


a graph.
Denoted
  
Gis the minimum number of edges whose
deletion disconnects G or makes G trivial.
An edge cutset contains edges whose removal disconnects the
graph.  1
A connected graph has a bridge if and only if
The above graph G1 can be The above graph G3 cannot be
split up into two components disconnected by removing a
by removing one of the single edge, but the removal of
edges bc or bd. Therefore, two edges (such as ac and bc)
edge bc or bd is a bridge. disconnects it.
Vertex Connectivity
Denoted   G  is the minimum number of vertices
whose deletion disconnects G or makes G trivial.
 
If G is disconnected then  G  0
A vertex cutset contains vertices whose removal
disconnects the graph.
A graph G is called k-connected for some positive
 
integer k if  G  k
 
G has a cut vertex if and only if  G  k
The same terminology applies to the edges too.
The above graph G can be The above G cannot be
disconnected by removal of disconnected by removing a
single vertex (either b or c). The single vertex, but the removal
G has connectivity 1. of two non-adjacent vertices
(such as b and c) disconnects
it. The G has connectivity 2.
Example
Find a minimal vertex cutset and minimal edge cutset.

a
d

e c
b
h

f g j
i
Graph Coloring Problem
 Graph coloring is an assignment of "colors", almost always
taken to be consecutive integers starting from 1 without loss of
generality, to certain objects in a graph. Such objects can be
vertices, edges, faces, or a mixture of the above.

 Application examples: scheduling, register allocation in a


microprocessor, frequency assignment in mobile radios, and
pattern matching
Vertex Coloring Problem

 Assignment of colors to the vertices of the graph such that proper


coloring takes place (no two adjacent vertices are assigned the same
color)
 Chromatic number: least number of colors needed to color the graph
 A graph that can be assigned a (proper) k-coloring is k-colorable, and it
is k-chromatic if its chromatic number is exactly k.
Vertex Coloring Problem
 The problem of finding a minimum coloring of a graph is NP-Hard
 The corresponding decision problem (Is there a coloring which uses at
most k colors?) is NP-complete
 The chromatic number for Cn = 3 (n is odd) or 2 (n is even), Kn = n, Km,n = 2
 Cn: cycle with n vertices; Kn: fully connected graph with n vertices; Km,n:
complete bipartite graph

C4 C5
K4 K2, 3
Vertex Covering Problem
 The Four color theorem: the chromatic number of a planar
graph is no greater than 4
 Example: G1 chromatic number = 3, G2 chromatic number = 4
 (Most proofs rely on case by case analysis).

G1 G2
Euler Paths and Circuits
The Seven bridges of Königsberg

C
c

D
A
a d

B
b
Euler Paths and Circuits

An Euler path is a path using every


edge of the graph G exactly once.
An Euler circuit is an Euler path that
returns to its start.
C

Does this graph have an Euler circuit?


A D

N
o B
.
Necessary and Sufficient Conditions

How about multigraphs?

A connected multigraph has a Euler


circuit iff each of its vertices has an even
degree.
A connected multigraph has a Euler
path but not an Euler circuit iff it has
exactly two vertices of odd degree.
Example
Which of the following graphs has an
Euler circuit?
a b a b a b

e e

d c d c c d e

yes no no
(a, e, c, d, e, b, a)
Example
Which of the following graphs has an
Euler path?
a b a b a b

e e

d c d c c d e

yes no yes
(a, e, c, d, e, b, a ) (a, c, d, e, b, d, a, b)
Euler Circuit in Directed Graphs

NO (a, g, c, b, g, e, d, f, a) NO
Euler Path in Directed Graphs

NO (a, g, c, b, g, e, d, f, a) (c, a, b, c, d, b)
Hamilton Paths and Circuits

A Hamilton path in a graph G is a path


which visits every vertex in G exactly
once.
A Hamilton circuit is a Hamilton path
that returns to its start.
Hamilton Circuits

Dodecahedron puzzle and it equivalent graph

Is there a circuit in this graph that passes through


each vertex exactly once?
Hamilton Circuits

Yes; this is a circuit that passes through each vertex


exactly once.
Finding Hamilton Circuits

Which of these three figures has a Hamilton circuit?


Of, if no Hamilton circuit, a Hamilton path?
Finding Hamilton Circuits

• G1 has a Hamilton circuit: a, b, c, d, e, a


• G2 does not have a Hamilton circuit, but does
have a Hamilton path: a, b, c, d
• G3 has neither.
Finding Hamilton Circuits

Unlike the Euler circuit problem,


finding Hamilton circuits is hard.
There is no simple set of necessary and
sufficient conditions, and no simple
algorithm.
Properties to look for ...
No vertex of degree 1
If a node has degree 2, then both edges
incident to it must be in any Hamilton
circuit.
No smaller circuits contained in any
Hamilton circuit (the start/endpoint of
any smaller circuit would have to be
visited twice).
Hamiltonian Graph
 DIRAC’S Theorem: if G is a simple graph with n
vertices with n ≥ 3 such that the degree of every
vertex in G is at least n/2 then G has a Hamilton
circuit.

 ORE’S Theorem: if G is a simple graph with n


vertices with n ≥ 3 such that deg (u) + deg (v) ≥ n for
every pair of nonadjacent vertices u and v in G, then
G has a Hamilton circuit.
Theorem : Euler's planar graph theorem
For a connected planar graph or multigraph:
v–e+r=2

number
number number of regions
of vertices of edges
Example of Euler’s theorem

A planar graph divides the plane


R1 into several regions (faces), one
K4 R2 R4
of them is the infinite region.

R3 v=4,e=6,r=4, v-e+r=2

Вам также может понравиться