MCA 202 Discrete Mathematics Instructor Neelima Gupta nguptacs

  • Slides: 62
Download presentation
MCA 202: Discrete Mathematics Instructor Neelima Gupta ngupta@cs. du. ac. in

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

Introduction to Graphs Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Introduction to Graphs Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 Two way Highways --- Undirected Graph Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of One Way Highways connecting Cities v 1 v 2 v 8 v

Map of One Way Highways connecting Cities v 1 v 2 v 8 v 7 v 6 v 5 One way Highways --- Directed Graph Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) v 3 v 4

Some of the Qs we often want to answer • Is there a path

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) Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways with cost of traveling v 1 1 5 v 2 v

Map of Highways with cost of traveling v 1 1 5 v 2 v 8 2 8 3 v 3 6 3 v 7 v 6 9 v 5 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 4 7 v 4

Qs we may want to answer • What is the cheapest path from city

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) Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

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

Connecting the Cities in a Cheapest Way (Minimum Spanning Tree) v 1 1 5 v 7 v 2 v 8 2 v 3 6 8 3 v 6 9 v 5 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 4 3 7 v 4

Form a network of Computers in a cheapest way (MST) 1 8 3 2

Form a network of Computers in a cheapest way (MST) 1 8 3 2 7 5 6 4 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

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

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. Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Directed Graphs V={v 1, v 2, …} E⊂Vx. V E: V V E ⊆

Directed Graphs V={v 1, v 2, …} E⊂Vx. 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. Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of One Way Highways connecting Cities v 1 v 2 v 8 v

Map of One Way Highways connecting Cities v 1 v 2 v 8 v 7 v 6 v 5 One way Highways --- Directed Graph Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) v 3 v 4

Undirected Graphs V={v 1, v 2, …} E is a collection of subsets of

Undirected Graphs V={v 1, v 2, …} 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. Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 Two way Highways --- Undirected Graph Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

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

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

2 D Array Representation • G=(V, E) v 1 v 2. . vn v

2 D Array Representation • G=(V, E) v 1 v 2. . vn v 1 v 2 ………………. vn G(Vi, Vj)=1 iff (Vi, Vj) Є E(G) Thanks, Neeharika Chaudhary, Roll No. : 26(MCA 2012)

Undirected Graph v 1 v 2 v 3 v 4 v 5 v 6

Undirected Graph v 1 v 2 v 3 v 4 v 5 v 6 0 1 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 0 0 v 2 v 1 v 6 v 4 v 3 v 5 MATRIX IS SYMMETRIC FOR AN UNDIRECTED GRAPH Thanks, Neeharika Chaudhary, Roll No. : 26(MCA 2012)

Directed Graph v 1 v 2 v 3 v 4 v 5 v 6

Directed Graph v 1 v 2 v 3 v 4 v 5 v 6 0 0 0 1 0 0 0 1 0 0 0 0 0 v 2 v 1 v 6 v 4 v 3 v 5 MATRIX IS NOT SYMMETRIC FOR A DIRECTED GRAPH Thanks, Neeharika Chaudhary, Roll No. : 26(MCA 2012)

Weighted Graphs V={v 1, v 2, …} W: E R • Weights could be

Weighted Graphs V={v 1, v 2, …} W: E R • Weights could be – cost of traveling – cost of connecting cities – cost of connecting computers Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Adjacency List Representation Thanks Neeraj Roll no-28(MCA 2012)

Adjacency List Representation Thanks Neeraj Roll no-28(MCA 2012)

For Undirected graphs 2 v 1 v 2 v 4 v 2 v 3

For Undirected graphs 2 v 1 v 2 v 4 v 2 v 3 v 1 v 3 v 2 v 4 v 1 v 3 v 5 v 4 v 6 v 1 3 v 6 1 4 v 6 6 Size =2 X No. of Edges =2|E| Thanks Neeraj Roll no-28(MCA 2012) 5

 • Size of Adjacency Matrix is theta (v 2). • We know that

• Size of Adjacency Matrix is theta (v 2). • We know that |E| < O (v 2). How? • When E<< v 2 /2 i. e. when the graph is sparse, then adjacency list representtaion saves a lot of space. Thanks Neeraj Roll no-28(MCA 2012)

For Directed Graphs 2 v 1 v 2 v 4 v 2 v 3

For Directed Graphs 2 v 1 v 2 v 4 v 2 v 3 v 1 v 3 v 2 v 4 v 1 v 3 v 5 v 4 v 6 v 1 3 v 6 1 4 Χ v 6 6 Size =|E| ∑ indeg(v)= ∑out deg(v)= |E| Thanks Neeraj Roll no-28(MCA 2012) 5

Map of Highways with cost of traveling v 1 1 5 v 2 v

Map of Highways with cost of traveling v 1 1 5 v 2 v 8 2 8 3 v 3 6 3 v 7 v 6 9 v 5 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 4 7 v 4

Multi - Graphs • A graph in which there could be multiple edges between

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. Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways with more than one way to reach directly • May be

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 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 Two way Highways --- Undirected Multi-Graph

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v

Map of Highways connecting Cities v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 Two way Highways --- Undirected Multi-Graph

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of edges Thanks: Meghna 22, Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6

Paths and Circuits Paths v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of edges Thanks: Meghna 22, Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits v 1 v 2 v 8 v 7 v 6 v

Paths and Circuits v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits v 1 v 2 v 8 v 7 v 6 v

Paths and Circuits v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits v 1 v 2 v 8 v 7 v 6 v

Paths and Circuits v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits in a Directed Graph Paths v 1 v 2 v 8

Paths and Circuits in a Directed Graph Paths v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of directed edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits in a Directed Graph Paths v 1 v 2 v 8

Paths and Circuits in a Directed Graph Paths v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of directed edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits in a Directed Graph Paths v 1 v 2 v 8

Paths and Circuits in a Directed Graph Paths v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of directed edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits in a Directed Graph Circuits v 1 v 2 v 8

Paths and Circuits in a Directed Graph Circuits v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of directed edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits in a Directed Graph Circuits v 1 v 2 v 8

Paths and Circuits in a Directed Graph Circuits v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of directed edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Paths and Circuits in a Directed Graph Circuits v 1 v 2 v 8

Paths and Circuits in a Directed Graph Circuits v 1 v 2 v 8 v 7 v 6 v 5 v 3 v 4 A path can be represented by a sequence of vertices or by a sequence of directed edges Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202)

Connected Graphs • An undirected graph G is said to be connected if for

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 uv. 1 u 2 u 4 u 3 u 5 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Example of a Disconnected Graph u 1 u 2 u 4 u 6 u

Example of a Disconnected Graph u 1 u 2 u 4 u 6 u 3 u 7 u 5 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) u 8

Directed Connected Graphs • A directed graph G is said to be weakly connected

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. Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Example of a strongly connected graph u 1 u 7 u 8 u 2

Example of a strongly connected graph u 1 u 7 u 8 u 2 u 4 u 5 Thanks: Meghna 22 , Mrityunjaya 23, Vibha Yadav 47 (MCA 202) u 3

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

Example of a graph that is weakly connected but not strongly connected u 1 u 8 u 5 u 7 u 6 u 4 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) u 2 u 3

Degree of a Vertex • In undirected graph G, degree(u) is defined as the

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. Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

For an undirected graph, = 2|E| where, V is the vertex set , E

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( v 1 )=0 which is true as the number of edges are zero and therefore, 2|E| =0. Thanks Neelakshi Dang Roll No. 27 (MCA 2012) v 1

For a graph with two vertex, v v 1 2 Case(i) : when both

For a graph with two vertex, v v 1 2 Case(i) : when both vertex are disconnected, = deg(v 1)+ deg(v 2)=0 which is true as the number of edges for the graph is zero and therefore, 2|E| =0. v 1 v 2 Case(ii): when both vertex are connected, = deg(v 1)+ deg(v 2)= 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)

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

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. - 1 Thanks Neelakshi Dang Roll No. 27 (MCA 2012)

Let deg(v*) = l , and let v 1 , v 2, … ,

Let deg(v*) = l , and let v 1 , v 2, … , vl be the neighbors of v* in G, then deg. G’(vi) = deg. G(vi) – 1 , i = 1, 2, … , l _ 2 and deg. G’(vi) = deg. G(vi) , v≠vi → |E(G’)| = |E(G)| - l From 1 and 2 3 , + → - = 2|E(G’)|+l Thanks Neelakshi Dang Roll No. 27 (MCA 2012) = 2|E(G’)| - 4

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

Adding deg(v*) to both sides of eq. 4 , we get = + deg. G(v*) = 2|E(G’)|+ l = 2|E(G’)|+ 2 l = 2|E(G’)+ l | = 2|E(G)| Hence Proved. Thanks Neelakshi Dang Roll No. 27 (MCA 2012) (using 3 )

Acyclic Graph: A Graph with no cycles A graph with no cycles Thanks: Meghna

Acyclic Graph: A Graph with no cycles A graph with no cycles Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

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

• A connected graph with no cycles is called a tree. Thanks Nakul kumar Roll No. 24, Navneet kumar Roll No. 25(MCA 2012)

Three equivalent statements about trees • T is a tree i. e. T is

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. Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

Claim- A tree with n vertices has exactly (n-1) edges. Proof-: Base Case: for

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. Thanks Nakul kumar Roll No. 24, Navneet kumar Roll No. 25(MCA 2012)

Let T be a tree with n+1 vertices. Thanks Nakul kumar Roll No. 24,

Let T be a tree with n+1 vertices. Thanks Nakul kumar Roll No. 24, Navneet kumar Roll No. 25(MCA 2012)

After removing this edge we get two separate trees T 1 and T 2,

After removing this edge we get two separate trees T 1 and T 2, such that E(T 1) U E(T 2) = E(T) – e and |V(T 1)| ≤ n and |V(T 2)| ≤ n |E(T 1)| = |V(T 1)|-1 (by induction hypothesis) |E(T 2)| = |V(T 2)|-1 ( by induction hypothesis) So, |E(T)| = |E(T 1)|+|E(T 2)|+1 |E(T)| = (|V(T 1)|-1) + (|V(T 2)|-1) + 1 = |V(T 1)| + |V(T 2)| - 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. 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

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’. 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.

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. Thanks Nakul kumar Roll No. 24, Navneet kumar Roll No. 25(MCA 2012)

Subgraphs Let G=(V, E) be a graph, then H=(V’, E’) is a subgraph of

Subgraphs 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’. v 1 v 2 v 8 v 7 v 6 v 5 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) v 3 v 4

Spanning Trees Given a graph G, a subgraph T of G is said to

Spanning Trees Given a graph G, a subgraph T of G is said to be a Spanning Tree if I. T is a tree II. T spans all the vertices of G. v 1 v 2 v 8 v 7 v 6 v 5 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) v 3 v 4