62
MCA 202: Discrete Mathematics Instructor Neelima Gupta [email protected]

MCA 202: Discrete Mathematics Instructor Neelima Gupta [email protected]

Embed Size (px)

Citation preview

Page 1: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

MCA 202: Discrete Mathematics

InstructorNeelima Gupta

[email protected]

Page 2: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Introduction to Graphs

Page 3: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways connecting Cities

v1v2

v3

v4v5

v6

v7

v8

Two way Highways --- Undirected Graph

Page 4: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of One Way Highways connecting Cities

v1v2

v3

v4v5

v6

v7

v8

One way Highways --- Directed Graph

Page 5: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Some of the Qs we often want to answer

• Is there a path from city ‘a’ to city ‘b’?• Is there a path to ‘c’ from ‘a’ via ’b’?• How can I visit all the cities and come back to

my original city without traveling any sector twice. (Euler Tour)

• Can I visit all the cities and come back to my original city without traveling any city twice? (Hamiltonian Circuit)

Page 6: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways with cost of traveling

v1v2

v3

v4v5

v6

v7

v8

1

4

25

3

7

6

9

83

Page 7: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Qs we may want to answer

• What is the cheapest path from city ‘a’ to city ‘b’? (Shortest Path Problem)

• Can I visit all the cities and come back to my original city without traveling any city twice? Can I get the cheapest such tour? (Traveling Salesman Problem)

Page 8: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Connecting the Cities in a Cheapest Way (Minimum Spanning Tree)

v1v2

v3

v4v5

v6

v7

v8

1

4

3

79

6

38

25

Page 9: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Form a network of Computers in a cheapest way (MST)

1

2

4

3

56

7

8

Page 10: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Graph

• A graph is a data structure to capture pairwise relationships amongst the objects.

• It is a collection of vertices and edges.• In the example of “map of highways

connecting cities”, cities can be represented by vertices and highways are binary relationships between cities which can be represented by edges.

Page 11: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Directed Graphs

V={v1, v2,…}

E V x V⊂E: V V

E {(u, v): u, v V }⊆ ∈E is a set of ordered pairs (u,v) of vertices from V representing an edge from u to v. v is stb the head of the arrow and u the tail.

Page 12: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of One Way Highways connecting Cities

v1v2

v3

v4v5

v6

v7

v8

One way Highways --- Directed Graph

Page 13: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Undirected Graphs

V={v1, v2,…}

E is a collection of subsets of V of size 2. • However, with a slight abuse of notation we’ll continue

to write (u,v) for an undirected edge instead of {u,v} and whether (u,v) is an ordered pair or not will be clear from the context.

• To capture self-loops, subsets are allowed to be multisets.

• The edge (u, v) is stb incident on u and v. • u and v are stb adjacent to each other.

Page 14: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways connecting Cities

v1v2

v3

v4v5

v6

v7

v8

Two way Highways --- Undirected Graph

Page 15: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Representation of Graphs

Thanks, Neeharika Chaudhary,Roll No.:26(MCA 2012)

Page 16: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

2D Array Representation

• G=(V,E) v1 v2 ………………. vn

v1 v2

. . vn

G(Vi,Vj)=1 iff (Vi,Vj) Є E(G)

Thanks, Neeharika Chaudhary,Roll No.:26(MCA 2012)

Page 17: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Undirected Graph

v2

v1

v6 v4 v3

v5

MATRIX IS SYMMETRIC FOR AN UNDIRECTED GRAPH

Thanks, Neeharika Chaudhary,Roll No.:26(MCA 2012)

v1 v2 v3 v4 v5 v6

0 1 0 1 0 1 1 0 1 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0

v1

v2

v3

v4

v5

v6

Page 18: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Directed Graph

v2

v1

v6 v4 v3

v5

MATRIX IS NOT SYMMETRIC FOR A DIRECTED GRAPHThanks, Neeharika Chaudhary,Roll No.:26(MCA 2012)

v1 v2 v3 v4 v5 v6

v1

v2

v3

v4

v5

v6

0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 19: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Weighted Graphs

V={v1, v2,…}

W: E R

• Weights could be– cost of traveling – cost of connecting cities– cost of connecting computers

Page 20: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Neeraj Roll no-28(MCA 2012)

Adjacency List Representation

Page 21: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Neeraj Roll no-28(MCA 2012)

For Undirected graphs

2 3

1 4

6 5

v1

v2

v3

v4

v5

v6

v2

v1

v1

v1

v2

v4 v6

v3

v4

v3 v6

v4

Size =2 X No. of Edges

=2|E|

Page 22: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Neeraj Roll no-28(MCA 2012)

• Size of Adjacency Matrix is theta (v2).• We know that |E| < O (v2). How?• When E<< v2 /2 i.e. when the graph is sparse,

then adjacency list representtaion saves a lot of space.

Page 23: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Neeraj Roll no-28(MCA 2012)

2 3

1 4

6 5

v1

v2

v3

v4

v5

v6

v2

v1

v1

v1

v2

v4 v6

v3

v4

v3 v6

v4

For Directed Graphs

Χ

Size =|E|

∑ indeg(v)= ∑out deg(v)= |E|

Page 24: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways with cost of traveling

v1v2

v3

v4v5

v6

v7

v8

1

4

25

3

7

6

9

83

Page 25: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Multi - Graphs

• A graph in which there could be multiple edges between a pair of vertices.

• For directed graph, a multi-graph would be the one allowing multiple edges from a vertex u to another vertex v.

Page 26: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways with more than one way to reach directly

• May be more than one highway.• One or more roadway, one or more railway,

one or more airway: each may have different cost in general

Page 27: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Map of Highways connecting Cities

v1v2

v3

v4v5

v6

v7

v8

Two way Highways --- Undirected Multi-Graph

Page 28: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Map of Highways connecting Cities

v1v2

v3

v4v5

v6

v7

v8

Two way Highways --- Undirected Multi-Graph

Page 29: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and CircuitsPaths

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22, Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of edges

Page 30: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and CircuitsPaths

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of edges

Page 31: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and CircuitsPaths

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of edges

Page 32: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and CircuitsPaths

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22, Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of edges

Page 33: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and CircuitsCircuits

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of edges

Page 34: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and CircuitsCircuits

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of edges

Page 35: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and CircuitsCircuits

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of edges

Page 36: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and Circuits in a Directed GraphPaths

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of directed edges

Page 37: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and Circuits in a Directed GraphPaths

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of directed edges

Page 38: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and Circuits in a Directed GraphPaths

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of directed edges

Page 39: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and Circuits in a Directed GraphCircuits

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of directed edges

Page 40: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and Circuits in a Directed GraphCircuits

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of directed edges

Page 41: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Paths and Circuits in a Directed GraphCircuits

v1v2

v3

v4v5

v6

v7

v8

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

A path can be represented by a sequence of vertices or by a sequence of directed edges

Page 42: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Connected Graphs

• An undirected graph G is said to be connected if for every u and v V there is a path ∈between u and v.

u1

u3u2

u5u4

Page 43: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Example of a Disconnected Graph

u1

u3u2

u5u4

u6

u7u8

Page 44: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Directed Connected Graphs

• A directed graph G is said to be weakly connected if after removing the direction the graph is connected.

• A directed graph G is said to be strongly connected if for every u, v V(G) a directed ∈ ∃path from u to v.

Page 45: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

u1 u2

u3

u5u8

u4u7

Thanks: Meghna 22 ,Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Example of a strongly connected graph

Page 46: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

u1 u2

u4 u3

u6u7

u5u8

Example of a graph that is weakly connected but not strongly connected

Page 47: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Degree of a Vertex

• In undirected graph G, degree(u) is defined as the number of edges incident on u.

• In a directed graph G, in-degree (u) is the number of edges with u as the arrow head.

• In a directed graph G, out-degree (v) is the number of edges with u as the tail of the arrow.

Page 48: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Vv

v)deg(For an undirected graph,

= 2|E|

where, V is the vertex set , E denotes the set of edges of the graph and deg(v) denotes the number of edges incident on vertex v.

Proof :Base case:For a graph with only one vertex,

deg( v1 )=0

which is true as the number of edges arezero and therefore, 2|E| =0.

Thanks Neelakshi Dang Roll No. 27 (MCA 2012)

v1

Page 49: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

For a graph with two vertex,

Case(i) :when both vertex are disconnected,

= deg(v1)+ deg(v2)=0

which is true as the number of edges for the graph is zero and therefore, 2|E| =0.Case(ii):when both vertex are connected,

= deg(v1)+ deg(v2)= 1+1 =2

which is true as the number of edges for the graph is one therefore, 2|E| = 2(1) = 2 .

Thanks Neelakshi Dang Roll No. 27 (MCA 2012)

v1

v2

Vv

v)deg(

v1

v2

Vv

v)deg(

Page 50: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Induction Hypothesis: Let the hypothesis hold for every graph of vertex size n,

Let G be a graph with (n+1) vertices. Remove a vertex v* and all the edges incident on it, i.e. remove deg(v*) edges incident on v*, resulting in a graph G’ with n vertices.

→ = 2|E(G’)| by I.H. -

Thanks Neelakshi Dang Roll No. 27 (MCA 2012)

)'(

)(degGVv

vG'

1

Page 51: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Let deg(v*) = l , and let v1 , v2, … , vl be the neighbors of v* in G, thendegG’(vi) = degG(vi) – 1 , i = 1,2, … , l _and degG’(vi) = degG(vi) , v≠vi

→ |E(G’)| = |E(G)| - l -

From and , + = 2|E(G’)|

→ = 2|E(G’)|+l -

Thanks Neelakshi Dang Roll No. 27 (MCA 2012)

2

3

1 2

)'(

)(degGVv

vG

liivv

vG,...,2,1,

)(deg

l

i

iv1

]1)[deg(

4

Page 52: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Adding deg(v*) to both sides of eq. , we get = + degG(v*)

= 2|E(G’)|+ l + l

= 2|E(G’)|+ 2l = 2|E(G’)+ l | = 2|E(G)| (using )

Hence Proved.

Thanks Neelakshi Dang Roll No. 27 (MCA 2012)

4

)(

)(degGVv

G v )'(

)(degGVv

vG

3

Page 53: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Acyclic Graph: A Graph with no cycles

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

A graph with no cycles

Page 54: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Nakul kumar Roll No.24,Navneet kumar Roll No.25(MCA 2012)

•A connected graph with no cycles is called a tree.

Page 55: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Three equivalent statements about trees

• T is a tree i.e. T is a connected acyclic graph.• T is a connected graph with n-1 edges.• There is a unique path between every pair of

vertices.

Page 56: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Nakul kumar Roll No.24,Navneet kumar Roll No.25(MCA 2012)

Claim- A tree with n vertices has exactly (n-1) edges.Proof-: Base Case: for n=1. no. of edges =0.For n=2 No. of edges=1.Therefore, it holds true for n=1 and n=2.Induction Hypothesis:Let it be true for every tree with ≤ n vertices.

Page 57: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Nakul kumar Roll No.24,Navneet kumar Roll No.25(MCA 2012)

Let T be a tree with n+1 vertices.

Page 58: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Nakul kumar Roll No.24,Navneet kumar Roll No.25(MCA 2012)

After removing this edge we get two separate trees T1 and T2, such that E(T1) U E(T2) = E(T) – e and

|V(T1)| ≤ n and |V(T2)| ≤ n

|E(T1)| = |V(T1)|-1 (by induction hypothesis)

|E(T2)| = |V(T2)|-1 ( by induction hypothesis)

So, |E(T)| = |E(T1)|+|E(T2)|+1

|E(T)| = (|V(T1)|-1) + (|V(T2)|-1) + 1

= |V(T1)| + |V(T2)| - 1

= | V(T)| - 1 = n+1-1 = n Hence proved.Ex: Prove the converse i..e a connected graph with n-1 edges is acyclic.

Page 59: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Nakul kumar Roll No.24,Navneet kumar Roll No.25(MCA 2012)

Exercise:Prove that T is a tree iff there exists a unique simple path between every pair of vertices.

Proof:Using the definition of tree, If T is a tree, it is connected, i.e there exists some path between every pair of vertices.Suppose if possible, there exists two distinct paths between some pair of vertices ‘u’ and ‘v’.

Page 60: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks Nakul kumar Roll No.24,Navneet kumar Roll No.25(MCA 2012)

Without loss of generality, we assume that there is no common vertex between them.

So, this shows there exists a cycle, which contradicts the definition of tree.Hence, a contradiction.

Assignment: Prove the converse.

Page 61: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Subgraphs

v1v2

v3

v4v5

v6

v7

v8

Let G=(V,E) be a graph, then H=(V’,E’) is a subgraph of G if E’ is a subset of E and V’ is a subset of V, such that u, v is in V’ , for all (u,v) in E’.

Page 62: MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs.du.ac.in

Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Spanning Trees

v1v2

v3

v4v5

v6

v7

v8

Given a graph G, a subgraph T of G is said to be a Spanning Tree ifI. T is a treeII. T spans all the vertices of G.