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

Slides:



Advertisements
Similar presentations
Chapter 5: Tree Constructions
Advertisements

Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Review Binary Search Trees Operations on Binary Search Tree
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.  Definition A simple graph G= (V, E) consists of vertices, V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Elementary Graph Algorithms
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs Representation, BFS, DFS
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs Part 2 Adjacency Matrix
Graphs By Rajanikanth B.
Graph Implementation.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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

Graphs and their representation

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

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

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

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

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

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.

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.

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.

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.

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

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

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

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