Professional Documents
Culture Documents
u v
u v
Definitions – Edge Type
Loop: A loop is an edge whose endpoints are equal
i.e., an edge joining a vertex to it self is called a
loop. Represented as {u, u} = {u}
u v
w
Definitions – Graph Type
Multigraph: G(V,E), consists of set of vertices V, set of
Edges E and a function f from E to {{u, v}| u, v V, u ≠ v}.
The edges e1 and e2 are called multiple or parallel edges if f
(e1) = f (e2).
Representation Example: V = {u, v, w}, E = {e1, e2, e3}
u
e1 e2
w
e3
v
Definitions – Graph Type
Directed Graph: G(V, E), set of vertices V, and set of Edges E,
that are ordered pair of elements of V (directed edges)
Representation Example: G(V, E), V = {u, v, w}, E = {(u, v), (v,
w), (w, u)}
u v
w
Definitions – Graph Type
Directed Multigraph: G(V,E), consists of set of vertices V, set
of Edges E and a function f from E to {{u, v}| u, v V}. The
edges e1 and e2 are multiple edges if f(e1) = f(e2)
Representation Example: V = {u, v, w}, E = {e1, e2, e3, e4}
u
w e4
e1 e2
v e3
Terminology – Undirected graphs
u and v are adjacent if {u, v} is an edge, e is called incident with u and v. u
and v are called endpoints of {u, v}
u v
k
w
Terminology – Directed graphs
For the edge (u, v), u is adjacent to v OR v is adjacent from u, u – Initial
vertex, v – Terminal vertex
u v
w
Theorems: Undirected Graphs
Theorem 1
The Handshaking theorem:
2e deg(v)
vV
Pr oof V 1 is the set of even degree vertices and V2 refers to odd degree vertices
2e deg(v) deg(u) deg(v)
vV u V1 v V2
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, obtained by adding additional vertex to Cn and
connecting all vertices to this new vertex by new edges.
Representation Example: W3, W4
W3 W4
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
V1 V2
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
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}},
H1 , 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
u
u
w v
w w v
G1 G2 G
Representation
Incidence (Matrix): Most useful when information about
edges is more desirable than information about vertices.
e1 e2 e3
u
v 1 0 1
e1 e2
u 1 1 0
v w w 0 1 1
e3
Representation- Adjacency Matrix
There is an N x N matrix, where |V| = N , the Adjacenct Matrix
(NxN) A = [aij]
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
Representation- Adjacency List
Each node (vertex) has a list of which nodes (vertex) it is adjacent
u
node Adjacency List
u v,w
v w, u
v w
w u,v
Graph - Isomorphism
G1 = (V1, E2) and G2 = (V2, E2) are isomorphic if:
There is a one-to-one and onto function f from V1 to
V2 with the property that
a and b are adjacent in G1 if and only if f (a) and f (b) are
adjacent in G2, for all a and b in V1.
Function f is called isomorphism
Application Example:
In chemistry, to find if two compounds have the same
structure
Graph - Isomorphism
Representation example: G1 = (V1, E1) , G2 = (V2, E2)
f(u1) = v1, f(u2) = v4, f(u3) = v3, f(u4) = v2,
u1 u2 v1 v2
u3 u4 v4
v3
Connectivity – Path
A Path is a sequence of edges that begins at a vertex
of a graph and travels along edges of the graph,
always connecting pairs of adjacent vertices.
2
1 v
3
u
4 5
Connectivity – Connectedness
Undirected Graph
An undirected graph is connected if there exists is a
simple path between every pair of vertices
v2 v5
Connectivity – Connectedness
Undirected Graph
Articulation Point (Cut vertex): removal of a vertex
produces a subgraph with more connected components than in
the original graph. The removal of a cut vertex from a
connected graph produces a graph that is not connected
Cut Edge: An edge whose removal produces a subgraph with
more connected components than in the original graph.
Representation example: G (V, E), v3 is the articulation point or
edge {v2, v3}, the number of connected components is 2 (> 1)
v3 v5
v1
v2
v4
Connectivity – Connectedness
Directed Graph
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
(undirected) path between every two vertices in the underlying
undirected path
G1 G2 G3
Connectivity – Connectedness
Directed Graph
Strongly connected Components: subgraphs of a
Graph G that are strongly connected
Representation example: G1 is the strongly
connected component in G
G G1
Isomorphism - revisited
A isomorphic invariant for simple graphs is the
existence of a simple circuit of length k , k is an
integer > 2 (why ?)
Representation example: G1 and G2 are isomorphic since we
have the invariants, similarity in degree of nodes, number of
edges, length of circuits
G1 G2
Counting Paths
Theorem: Let G be a graph with adjacency matrix A with respect to the
ordering v1, v2, …, Vn (with directed on undirected edges, with multiple
edges and loops allowed). The number of different paths of length r from
Vi to Vj, where r is a positive integer, equals the (i, j)th entry of
(adjacency matrix) Ar.
Base Case: For the case N = 1, aij =1 implies that there is a path of length 1. This
is true since this corresponds to an edge between two vertices.
We assume that theorem is true for N = r and prove the same for N = r +1.
Assume that the (i, j)th entry of Ar is the number of different paths of length r from
vi to vj. By induction hypothesis, bik is the number of paths of length r from vi to vk.
Counting Paths
Case r +1: In Ar+1 = Ar. A,
The (i, j)th entry in Ar+1 , bi1a1j + bi2 a2j + …+ bin anj
where bik is the (i, j)th entry of Ar.
The (i, j)th entry in Ar+1 corresponds to the length between i and j
and the length is r+1. This path is made up of length r from v i to vk
and of length from vk to vj. By product rule for counting, the number of
such paths is bik* akj The result is bi1a1j + bi2 a2j + …+ bin anj ,the desired
result.
Counting Paths
a ------- b
| |
| |
c -------d
A=0110 A4 = 8 0 0 8
1001 0880
1001 0880
0110 8008
You can redraw the original picture as long as for every edge between
nodes i and j in the original you put an edge between nodes i and j in
the redrawn version (and you put no other edges in the redrawn
version).
Original:
2 3
4
Redrawn: 2
4 1
3
The Seven Bridges of Königsberg, Germany
Euler:
a b
c d e
The problem in our language:
a b
f c d
c d
e
Delete the simple path:
{a,b}, {b,c}, {c,f}, {f,a}
c d
a b
f c d
“Splice” the circuits in the 2 graphs:
{a,b}, {b,c}, {c,f}, {f,a}
“+”
e {c,d}, {d,e}, {e,c}
“=“
{a,b}, {b,c}, {c,d}, {d,e}, {e,c}, {c,f}
{f,a}
Representation- Incidence Matrix
e1 e2 e3 e4 e5 e6 e7
e1
a b a 1 0 0 0 0 0 1
e2 b 1 1 0 0 0 0 0
e7
e3 c 0 1 1 0 1 1 0
f c d
d 0 0 1 1 0 0 0
e6
e5 e 0 0 0 1 1 0 0
e e4
f 0 0 0 0 0 1 1
Hamiltonian Graph
Hamiltonian path (also called traceable path) is a path that visits
each vertex exactly once.
Is it Hamiltonian?
Yes
.
Hamiltonian Graph
Application Example: VLSI design (overlapping edges requires extra layers), Circuit
design (cannot overlap wires on board)
K4
Planar Graphs
Representation examples: Q3
Planar Graphs
Representation examples: K3,3 is Nonplanar
v1 v2 v3 v1 v5 v1 v5
R21
R2 R1 R1
R22
v3
v4 v5 v6 v4 v2 v4 v2
Planar Graphs
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
Planar Graphs
Example of Euler’s theorem
R3 v=4,e=6,r=4, v-e+r=2
Planar Graphs
Proof of Euler’s formula: By Induction
Base Case: for G1 , e1 = 1, v1 = 2 and r1= 1
v
u
R1
an+1
bn+1
Planar Graphs
Case 2:
an+1
R
bn+1
Region Degree
R Degree of R = 3
Degree of R = ?
R
Planar Graphs
Elementary sub-division: Operation in which a graph are
obtained by removing an edge {u, v} and adding the vertex w
and edges {u, w}, {w, v}
u v u w v
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
G1 G2
Cut Vertex : Let ‘G’ be a connected graph. A vertex V ∈ G is
called a cut vertex of ‘G’, if ‘G-V’ (Delete ‘V’ from ‘G’) results in a
disconnected graph. Removing a cut vertex from a graph breaks it in to
two or more graphs.
Example
In the following graph, vertices ‘e’ and ‘c’ are the cut vertices.
By removing ‘e’ or ‘c’, the graph will become a
disconnected graph.
Without ‘g’, there is no path between vertex ‘c’ and vertex ‘h’ and many other. Hence
it is a disconnected graph with cut vertex as ‘e’. Similarly, ‘c’ is also a cut vertex for
the above graph.
Cut Edge (Bridge): Let ‘G’ be a connected graph. An edge ‘e’ ∈ G is called a cut edge
if ‘G-e’ results in a disconnected graph.
If removing an edge in a graph results in to two or more graphs, then that edge is
called a Cut Edge.
Example
In the following graph, the cut edge is [(c, e)]
By removing the edge (c, e) from the graph, it becomes a disconnected graph.
In the above graph, removing the edge (c, e) breaks the graph into two which is
nothing but a disconnected graph. Hence, the edge (c, e) is a cut edge of the graph.
Cut Set of a Graph :
Let ‘G’= (V, E) be a connected graph. A subset E’ of E is called a cut set of G if deletion
of all the edges of E’ from G makes G disconnect.If deleting a certain number of edges
from a graph makes it disconnected, then those deleted edges are called the cut set of
the graph.
Example:Take a look at the following graph. Its cut set is E1 = {e1, e3, e5, e8}.
After removing the cut set E1 from the graph, it would appear as follows −
Similarly there are other cut sets that can disconnect the graph −
branches
root
Computer Scientist’s View
root
leaves
branches
nodes
What is a Tree
A tree is a finite nonempty set of elements.
It is an abstract model of a hierarchical structure.
consists of nodes with a parent-child relation.
Applications:
Organization charts Computers”R”Us
File systems
Programming environments
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Finding Minimal Spanning
Tree
Kruskal’s algorithm
Prim’s algorithm
Example:
2
Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A
F
B
C
D
E
F
G
Example:
2
Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B 2 A
C 2 A
D 1 A
E
F
G
Example: Prim's Algorithm
2
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B 2 A
C 21 AD
D Y 1 A
E 1 D
F 6 D
G 5 D
Example: Prim's Algorithm
2
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B 2 A
C Y 21 AD
D Y 1 A
E 1 D
F 62 DC
G 5 D
Example: Prim's Algorithm
2
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B 21 AE
C Y 21 AD
D Y 1 A
E Y 1 D
F 62 DC
G 53 DE
Example: Prim's Algorithm
2
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B Y 21 AE
C Y 21 AD
D Y 1 A
E Y 1 D
F 62 DC
G 53 DE
Example: Prim's Algorithm
2
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B Y 21 AE
C Y 21 AD
D Y 1 A
E Y 1 D
F Y 62 DC
G 53 DE
Example: Prim's Algorithm
2
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B Y 21 AE
C Y 21 AD
D Y 1 A
E Y 1 D
F Y 62 DC
G Y 53 DE
Example: Prim's Algorithm
Output:
2 (A, D) (C, F)
A B 1 (B, E) (D, E)
1 (C, D) (E, G)
2 5 E
1
1 D Total Cost: 9
C 5 3
6 G vertex known? cost prev
10
2 A Y 0 -
F
B Y 21 AE
C Y 21 AD
D Y 1 A
E Y 1 D
F Y 62 DC
G Y 53 DE
MST – Minimum Spanning Tree
What greedy algorithm might we use
assuming
1 2
we start with the entire graph
1 2 3
6 5
4 4 6
3 8
4 5 6
7
4 3
7
96
Kruskal’s Algorithm
Make a disjoint set for each vertex
1 2
1 2 3 {1}{2}{3}{4}{5}{6}{7}
6 5
4 4 6
3 8
4 5 6
7
4 3
7
Kruskal’s Algorithm
Sort edges by weight
1 2
1 2 3 1: {1,2} {1}{2}{3}{4}{5}{6}{7}
5
2: {2,3}
6
4 4 6 3: {4,5}
3 8 3: {6,7}
4 5 6 4: {1,4}
7
4: {2,5}
4 3 4: {4,7}
5: {3,5}
7
Kruskal’s Algorithm
1 2
1 2 3 1: {1,2} {1}{2}{3}{4}{5}{6}{7}
5
2: {2,3}
6
4 4 6 3: {4,5}
3 8 3: {6,7}
4 5 6 4: {1,4}
7
4: {2,5}
4 3 4: {4,7}
5: {3,5}
7
99
Kruskal’s Algorithm
Merge vertices in added edges
1 2
1 2 3 1: {1,2}{1,2}{3}{4}{5}{6}{7}
5
2: {2,3}
6
4 4 6 3: {4,5}
3 8 3: {6,7}
4 5 6 4: {1,4}
7
4: {2,5}
4 3 4: {4,7}
5: {3,5}
7
103
Kruskal’s Algorithm
1 2
1 2 3 1: {1,2}{1,2}{3}{4}{5}{6}{7}
5
2: {2,3} {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: {4,5} {1,2,3}{4,5}{6}{7}
3 8 3: {6,7} {1,2,3}{4,5}{6,7}
4 5 6 4: {1,4} {1,2,3,4,5}{6,7}
7
4: {2,5}
4 3 4: {4,7}
5: {3,5}
7
104
Kruskal’s Algorithm
Must join separate components
1 2
1 2 3 1: {1,2}{1,2}{3}{4}{5}{6}{7}
5
2: {2,3} {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: {4,5} {1,2,3}{4,5}{6}{7}
3 8 3: {6,7} {1,2,3}{4,5}{6,7}
4 5 6 4: {1,4} {1,2,3,4,5}{6,7}
4: {2,5} rejected
7
4 3 4: {4,7}
5: {3,5}
7
105
Kruskal’s Algorithm
Done when all vertices in one set
Then they are all connected
Exactly |V| - 1 edges
1 2
1 2 3 1: {1,2}{1,2}{3}{4}{5}{6}{7}
5
2: {2,3} {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: {4,5} {1,2,3}{4,5}{6}{7}
3 8 3: {6,7} {1,2,3}{4,5}{6,7}
4 5 6 4: {1,4} {1,2,3,4,5}{6,7}
4: {2,5} rejected
7
4 3 4: {4,7}{1,2,3,4,5,6,7} done
5: {3,5}
7
106
Prim’s Algorithm
Choose arbitrary starting vertex
1 2
1 2 3 S = {5}
1: ∞
6 5 2: ∞
4 4 6
3: ∞
3 8 4: ∞
4 5 6 5: 0
6: ∞
8 7: ∞
4 3
7
Prim’s Algorithm
Choose arbitrary starting vertex
1 2
1 2 3 X : S = {5}
1: ∞
6 5 2: 4
4 4 6
3: 5
3 8 4: 3
4 5 6 6: 8
7: 8
8
4 3
108
Prim’s Algorithm
Pick shortest edge to leave S
1 2
1 2 3 X : S = {5}
1: ∞
5
{4,5} 2: 4
6
4 4 6
3: 5
3 8 4: 3
4 5 6 6: 8
7: 8
8
4 3
109
Prim’s Algorithm
Could use prev pointer to add edge
to X. Don’t actually store S.
Current PQ is always V - S
1 2
1 2 3 X : S = {5}
1: ∞
5
{4,5} S ={4,5} 2: 4
6
4 4 6
3: 5
3 8 4: 3
4 5 6 6: 8
7: 8
8
4 3
110
Prim’s Algorithm
Now choose shortest path from any
node in S – node on front of PQ.
Once popped off PQ, will never be
chosen as sink of an edge
1 2
1 2 3 X : S = {5}
1: 4
5
{4,5} S = {4,5} 2: 4
6
4 4 6
3: 5
3 8 6: 8
4 5 6 7: 4
8
4 3
111
Prim’s Algorithm
Repeat until PQ is empty
1 2
1 2 3 X : S = {5}
2: 1
6 5
{4,5} S= 3: 5
4 4 6 {1,4} {4,5}
S = {1,4,5} 6: 8
3 8 7: 4
4 5 6
8
4 3
112
Prim’s Algorithm
Repeat until PQ is empty
1 2
1 2 3 X : S = {5}
3: 2
6 5
{4,5} S= 6: 8
4 4 6 {1,4} {4,5}
S = {1,4,5} 7: 4
3 8 {1,2} S = {1,2,4,5}
4 5 6
8
4 3
113
Prim’s Algorithm
Repeat until PQ is empty
1 2
1 2 3 X : S = {5}
6: 6
6 5
{4,5} S= 7: 4
4 4 6 {1,4} {4,5}
S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
8
4 3
114
Prim’s Algorithm
Repeat until PQ is empty
1 2
1 2 3 X : S = {5}
6: 3
6 5
{4,5} S=
4 4 6 {1,4} {4,5}
S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
{4,7} S = {1,2,3,4,5,7}
8
4 3
115
Prim’s Algorithm
Repeat until PQ is empty
1 2
1 2 3 X : S = {5}
6 5
{4,5} S=
4 4 6 {1,4} {4,5}
S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
{4,7} S = {1,2,3,4,5,7}
8
4 3 {6,7} S = {1,2,3,4,5,6,7}
7
116