Presentation is loading. Please wait.

Presentation is loading. Please wait.

MCA 202: Discrete Mathematics Instructor Neelima Gupta

Similar presentations


Presentation on theme: "MCA 202: Discrete Mathematics Instructor Neelima Gupta"— Presentation transcript:

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

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

3 Map of Highways connecting Cities v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) Two way Highways ---  Undirected Graph

4 Map of One Way Highways connecting Cities v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) One way Highways ---  Directed Graph

5 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)

6 Map of Highways with cost of traveling v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 1 4 2 5 3 7 6 9 8 3

7 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)

8 Connecting the Cities in a Cheapest Way (Minimum Spanning Tree) v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 1 4 3 79 6 3 8 2 5

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

10 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)

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

12 Map of One Way Highways connecting Cities v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) One way Highways ---  Directed Graph

13 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)

14 Map of Highways connecting Cities v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) Two way Highways ---  Undirected Graph

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

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

17 Undirected Graph 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) v 1 v 2 v 3 v 4 v 5 v 6 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 v1v2v3v4v5v6v1v2v3v4v5v6

18 Directed Graph 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) v 1 v 2 v 3 v 4 v 5 v 6 v1v2v3v4v5v6v1v2v3v4v5v6 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

19 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)

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

21 Thanks Neeraj Roll no-28(MCA 2012) For Undirected graphs 2 3 1 4 6 5 v1 v2 v3 v4 v5 v6 v2 v1 v2 v4v6 v3 v4 v3 v6 v4 Size =2 X No. of Edges =2|E|

22 Thanks Neeraj Roll no-28(MCA 2012) 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.

23 Thanks Neeraj Roll no-28(MCA 2012) 2 3 1 4 6 5 v1 v2 v3 v4 v5 v6 v2 v1 v2 v4v6 v3 v4 v3 v6 v4 For Directed Graphs Χ Size =|E| ∑ in deg(v)= ∑ out deg(v)= |E|

24 Map of Highways with cost of traveling v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 1 4 2 5 3 7 6 9 8 3

25 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)

26 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)

27 Map of Highways connecting Cities v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Two way Highways ---  Undirected Multi-Graph

28 Map of Highways connecting Cities v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Two way Highways ---  Undirected Multi-Graph

29 Paths and Circuits Paths v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

30 Paths and Circuits Paths v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

31 Paths and Circuits Paths v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

32 Paths and Circuits Paths v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

33 Paths and Circuits Circuits v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

34 Paths and Circuits Circuits v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

35 Paths and Circuits Circuits v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

36 Paths and Circuits in a Directed Graph Paths v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

37 Paths and Circuits in a Directed Graph Paths v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

38 Paths and Circuits in a Directed Graph Paths v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

39 Paths and Circuits in a Directed Graph Circuits v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

40 Paths and Circuits in a Directed Graph Circuits v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

41 Paths and Circuits in a Directed Graph Circuits v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 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

42 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. u1u1 u3u3 u2u2 u5u5 u4u4 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

43 Example of a Disconnected Graph u1u1 u3u3 u2u2 u5u5 u4u4 u6u6 u7u7 u8u8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

44 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)

45 u1u1 u2u2 u3u3 u5u5 u8u8 u4u4 u7u7 Thanks: Meghna 22,Mrityunjaya 23, Vibha Yadav 47 (MCA 202) Example of a strongly connected graph

46 u1u1 u2u2 u4u4 u3u3 u6u6 u7u7 u5u5 u8u8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) Example of a graph that is weakly connected but not strongly connected

47 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)

48 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) v1v1

49 For a graph with two vertex, 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. 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) v1v1 v2v2 v1v1 v2v2

50 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) 1

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

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

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

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

55 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)

56 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)

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

58 After removing this edge we get two separate trees T1 and T2, 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)

59 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)

60 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)

61 Subgraphs v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 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’.

62 Spanning Trees v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202) 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.


Download ppt "MCA 202: Discrete Mathematics Instructor Neelima Gupta"

Similar presentations


Ads by Google