Presentation is loading. Please wait.

Presentation is loading. Please wait.

Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.

Similar presentations


Presentation on theme: "Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By."— Presentation transcript:

1 Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By

2 Graphs and their representation

3 Graph Graph Defn 1: A graph (or undirected graph) consists of (a) a non empty set V is called the set of vertices (a) a non empty set V is called the set of vertices (b) a set E called the set of edges and (b) a set E called the set of edges and (c) a map Ø which assigns to every edge a unique (c) a map Ø which assigns to every edge a unique unordered pair of vertices unordered pair of vertices Defn 2: A directed graph (or digraph) consists of (a) a non empty set V is called the set of vertices (a) a non empty set V is called the set of vertices (b) a set E called the set of edges and (b) a set E called the set of edges and (c) a map Ø which assigns to every edge a unique (c) a map Ø which assigns to every edge a unique ordered pair of vertices ordered pair of vertices

4 Graph Graph vv3vv3 vv1vv1 vv4vv4 vv2vv2 e1e1 e2e2 e3e3 e4e4 e5e5 e6e6 vv3vv3 vv1vv1 vv4vv4 vv2vv2 e1e1 e2e2 e3e3 e4e4 e5e5 e6e6 Undirected GraphDirected Graph

5 Graph Graph An edge is called a self loop if the vertices in its associated pair coincide If in a digraph an ordered pair (v, v) is associated with an edge e then v is called a predecessor of v where as v is called a successor of v If in a digraph an ordered pair (v i, v j ) is associated with an edge e then v i is called a predecessor of v j where as v j is called a successor of v i The degree of a vertex v in a graph is the no. of edges with v as an end vertex (A self loop is counted twice) A graph is connected if there is a path between every pair of vertices

6 Graph Graph A circuit in a graph is an alternating sequence veve……ev of vertices and edges starting and ending in the same vertex such that e has v and v as end vertices and no edge or vertex other than v is repeated A circuit in a graph is an alternating sequence v 1 e 1 v 2 e 2 ……e n-1 v 1 of vertices and edges starting and ending in the same vertex such that e i has v i and v i+1 as end vertices and no edge or vertex other than v 1 is repeated

7 Graph Graph Graph Traversal Visiting all the nodes in a graph systematically is called “Traversal”. Two most common methods of traversal : Depth First Search (DFS) & Breadth First Search (BFS) 2 1 3 0 4 Graph G3

8 Graph Graph DFS Step 1: Choose any node in the graph. Design it as the search node and mark it as visited Step 2: Find a node that is adjacent to the search node using the adjacency matrix of the graph. This node must not have been visited before. Designate this as the new search node (remembering the previous one) and mark it as visited. Step 3: Repeat Step 2. using new search node. When all the adjacent nodes to a search node have been visited, return to the previous search node (remembered it) and continue from there.

9 Graph Graph Step 4: When a return to the previous search node in Step 3. is impossible, the search from the originally chosen search node is complete. Step 5: If the graph still contains unvisited nodes, choose any node that has not been visited and repeat Steps 1. to 4.. The algorithm is called a depth first search because the search continues progressively deeper in a recursive manner.

10 Graph Graph BFS Step 1: Begin with any node, mark it as visited Step 2: Proceed to the next node having an edge connected to the node in Step 1. Mark it as visited. Step 3: Come back to the node in Step 1., descend along an edge toward an unvisited node, and mark the new node as visited.

11 Graph Graph Step 4. Repeat Step 3. until all nodes adjacent to the node in Step 1. have been marked as visited. Step 5. Repeat Steps 1. to 4. starting from the node visited in Step 2., then starting from the nodes visited in Step 3.in the order visited. Keep this up as long as possible before starting a new scan.

12 Tree Tree A graph (directed or undirected) is called a tree if it is connected and has no circuits vv2vv2 vv1vv1 vv4vv4 e1e1 e3e3 vv3vv3 e2e2 vv2vv2 vv1vv1 vv4vv4 e1e1 e3e3 vv3vv3 e2e2 Undirected TreeDirected Tree

13 Tree Tree In a tree there is one & only one path between every pair of vertices If in a graph there is a unique path between every pair of vertices A tree with n vertices has n-1 edges. If a connected graph with n vertices has n-1 edges then it is a tree If a graph with no circuits has n vertices & n-1 edges then it is a tree A in a tree can be defined as a vertex or node of degree one. Vertices other than leaves are called or nodes A leaf in a tree can be defined as a vertex or node of degree one. Vertices other than leaves are called internal vertices or nodes

14 Tree Tree An ordered directed tree is a directed graph satisfying the following conditions: 1) There is one vertex or node called the root of the tree which is distinguished from all the other vertices & the root has no predecessors 2) There is a directed path from the root to every other vertex or node 3) Every vertex or node except the root has exactly one predecessor

15 Binary Tree Binary Tree A binary tree is a tree in which the degree of the root is 2 and the remaining vertex are of degree 1 or 3 The no. of vertices in a binary tree is odd A son of a vertex v is a successor of v A father of a vertex v is a predecessor of v The no. of edges in a path is called the length of the path The height of a tree is the length of a longest path from the root A vertex v in a tree is at level k if there is a path of length k from the root to the vertex v


Download ppt "Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By."

Similar presentations


Ads by Google