Graph Theory: Dept of Computer Science and Engineering Osmania University College of Engineering Hyderabad 500007
Graph Theory: Dept of Computer Science and Engineering Osmania University College of Engineering Hyderabad 500007
1. What Is a Graph?
2. Kinds of Graphs
3. Vertex Degree
4. Paths and
Cycles
Graph Theory 3
The KÖnigsberg Bridge Problem
• Königsber is a city on the Pregel river in
Prussia
• The city occupied two islands plus areas on
both banks
• Problem:
Whether they could leave home, ever
y
cross
X
bridge exactly once, and return
W Y
home.
Graph Theory 4
A Model
• A vertex : an island
• An edge : a path(bridg betwee tw
islands e) n o
X
e1
X
e2 e6
W Y
W Y e5
e4
e3 e7
Z
Z
Graph Theory 5
General Model
• A vertex : an
object
• An edge : a between object
relation two s
Committee 1 Committee 2
common
member
Graph Theory 6
What Is a Graph?
X
e1
e2 e6
W
e5 Y
e4
e3 e7
Graph Theory
Z 7
What Is a Graph?
Graph Theory 8
Loop, Multiple edges
•
Loop : An edge whose endpoints are
equal
• Multiple edges : have sam
Edges
pai of endpoint the e
r s
Multiple
edges
loop
Graph Theory 9
Simple Graph
Multiple
edges loop
Graph Theory 10
Adjacent, neighbors
• Two vertices are and
neighbors are
adjacent if they are the endpoint of an
edge s
• Example:
– A and B are adjacent
– A and D are not adjacent
A B
C D
Graph Theory 11
Finite Graph, Null Graph
Graph Theory 12
Connected and Disconnected
• Connected : There exists leas on
path between two vertices t
at e
• Disconnecte : Otherwise
•d
– H1 and H2 are connecte
Example:
d
– H3 is disconnected
a b a b
H1 c
H2 H3 c
e
d d
e d
Graph Theory 13
Complete Graph
Graph Theory 14
Sparse/Dense Graph
Graph Theory 15
Directed Graph (digraph)
Graph Theory 16
Weighted Graph
1.5
4
Graph Theory 17
5
Planar Graph
Graph Theory 18
Complement
Complement of G: The compleme G’ of
a simple graph G : nt
– A simple graph
– V(G’) = V(G)
– E(G’) = { uv | uv E(G) }
u
u
y
y v v
G
G’
w x w
x
Graph Theory 19
Subgraphs
Graph Theory 21
Subgraphs
G c
e d
a b
a b
c H3 c
H1 H2
d e d
e d
Graph Theory 22
Bipartite Graphs
• A graph G is bipartite if V(G) is the union of
two disjoint independent sets called partite
Also: The vertices can be partitioned
sets of G
into
• two sets such that each set is
• Job Assignme Proble
independent
nt m
Workers
Matching
Boys Problem
Girls Jobs
Graph Theory 23
Chromatic Number
Red Blue
common
member
common
member
• Scheduling problem is to
equivalent
grap coloring
h problem
Committee 2 Common
Common Member
Member Different Color
Committee 1
Committee 3
No Common Member
Same Color OK
Same time slot OK
A B C
D E F
The degree of B is 2.
4 5
4 5 6
Simple path: [ 1, 2, 4, 5 ]
Path: [ 1, 2, 4, 5, 4]
Circuit: [ 1, 2, 4, 5, 4, 1]
1 3
2
4 5 6
Cycle
• Hamilton path: a path that visits each vertex in a network exactly once
34
Euclerian Path
• An undirected graph possesses an Euclerian Path
if and only if it is connected and has either zero or
two vertices of odd degree
OR
• An undirected graph possesses an Euclerian Path
if and only if it is connected and its vertices are all
of even degree
• Adjacency Matrix
• Incidence Matrix
• Adjacency List
ei
vj vk
1 2 1 2 3 4 5
3 1 0 1 0 0 1
5 4 2 1 0 1 1 1
3 0 1 0 1 0
4 0 1 1 0 1
5
1 1 0 1 0
5
1 2 1 2 3 4 5
4
1 3 6 3 1 0 5 0 0 1
5 4 2 2 5 0 4 6 3
7
3 0 4 0 2 0
4 0 6 2 0 7
5
1 3 0 7 0
• Adjacency-list representation
– an array of |V | elements, one for each vertex in V
– For each u V , ADJ [ u ] points to all its adjacent
vertices.
1
2 5
1 2 2 5 3 4
3 3 4
5 4 4 5
5
5
• Kruskal's Algorithm
• Prim's Algorithm
o o o
r r r
The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that
graph.
2 2
5 3 3
1 1
This algorithm creates a forest of trees. Initially the forest consists of n single
node trees (and no edges). At each step, we add one edge (the cheapest
one) so that it joins two trees together. If it were to form a cycle, it would
simply link two nodes that were already part of a single connected tree, so
that this edge would not be needed.
Every step will have joined two trees in the forest together, so that at the end,
there will only be one tree in T.
4
B C
4
2 1
A 4 E
1 F
D 2 3
10
G 5
5 6 3
4
I
H
2 3
J
B 4 C B 4 D
4 10 2
B C B J C E
4
2 1
C 1 F D 5 H
A 4 E
1 F
2 D 6 J E 2 G
D 3
10
G 5
F 3 G F 5 I
5 6 3
4
I G 3 I G 4 J
H
2 3
J H 2 J I 3 J
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Don’t Add
Edge 2 2
C E E G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
Don’t Add
Edge 2 2
C E E G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
C 2 E E 2 G
4 2 3
B C H J F G
4
2 1
G 3 I I 3 J
A 4 E
1 F
2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3
4
I F 5 I D 5 H
H
2 3
J D 6 J B 10 J
4
B C B 4 C
4 4
2 1 2 1
A E A 4
1 F E F
1
D 2 2
D 3
10
G G 5
3 5 6 3
4
I I
H H
2 3 3
J 2 J
This algorithm starts with one node. It then, one by one, adds a node that
is unconnected to the new graph, each time selecting the node whose
connecting edge has the smallest weight out of the available nodes’
connecting edges.
1. The new graph is constructed - with one node from the old graph.
2. While new graph has fewer than n nodes,
1. Find the node from the old graph with the smallest connecting
edge to the new graph,
2. Add it to the new graph
Every step will have joined one node, so that at the end we will have one
graph with all the nodes and it will be a minimum spanning tree of the
original graph.
4
B C
4
2 1
A 4 E
1 F
D 2 3
10
G 5
5 6 3
4
I
H
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
4
B C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A E F
1
D 2 3
10 D 2
G 5
G
5 6 3
3
4
I
H I
3 H
2 J
2 3
J
• Flow of material
– liquid flowing through pipes
–
current through electrical networks
–
information through networks
–
communication parts through an
• In Operating systems to model resource handling
assembly line
(deadlock problems)
• In compilers for and optimizing the code.
parsing
Space O (bd)
O ( bd )
(ignoring 1 in comparison to b)
O ( bd )
(ignoring 1 in comparison to b)
1. d1
2. result depth first (initial state, d)
3. (Comment: try to find a solution of length d using depth first
search)
4.
If result ≠ NIL, report it, stop
5.
d d+ 1
6.
go to 2
DFID(1) = b1
DFID(k) = + DFID(k-1)
This expands to
For large k the above expression asymptotes to bk (as the expression in the parenthesis
asymptotes
Graph Theory to 1). Hence time required for DFIDFatima
S Sameen is O(bk) 115
Examples
• Flow of material
– liquid flowing through pipes
–
current through electrical networks
–
information through networks
–
communication parts through an
• In Operating systems to model resource handling
assembly line
(deadlock problems)
• In compilers for and optimizing the code.
parsing