C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Review Binary Search Trees Operations on Binary Search Tree
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Graphs.
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.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
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.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods.
Data Structures Using C++ 2E
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
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.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
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++
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.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
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.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
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.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures Graphs - Terminology
Basic Concepts Graphs For more notes and topics visit:
CS120 Graphs.
Graph Algorithm.
Spanning Trees.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graphs.
Graph Implementation.
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:

C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs

C o n f i d e n t i a l Developed By Nitendra Graphs Recap Definition of Graph Type of Graphs Graph Representation An adjacency List Representation An adjacency Matrix Representation Graph Traversal Breath First Search (BFS) - Algorithm Depth First Search (DFS) - Algorithm Spanning Tree Kruskal’s Algorithm Prim’s Algorithm Class summary NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Recap Definition of Tree Terminologies Type of Trees – Complete Binary Tree – Full/Strictly Binary Tree – Binary Search Tree –Threaded Binary Tree – Red Black Tree – B Tree [Balance Tree] – AVL Tree Binary Tree Traversals Application of Trees HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Definition of Graph : A graph is a set of objects called points, nodes, or vertices connected by links called lines or edges. Mathematically we can represent graph as A graph G is defined as follows: G=(V,E), where V is a finite, non-empty set of vertices (singular: vertex) and E is a set of edges (links between pairs of vertices). Graphs Graph G HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Type of Graphs Graphs Graph G2 Undirected Graph: When the edges in a graph have no direction, the graph is called undirected. Directed Graph (Digraph) : When the edges in a graph have direction, the graph is called undirected. Graph G1 HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Graph G4 Strongly Connected Graph: Two vertices are said to be connected if there is a path between them. Furthermore, a directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph. Weakly Connected Graph: A directed graph is weakly connected if at least two vertices are not connected. Graph G3 HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Degree of a vertex : The degree of a vertex is the number of lines incident to it. - The outdegree of a vertex in a digraph is the number of arcs leaving the vertex. - The indegree is the number of arcs entering the vertex. Graph G3 The indegree of the vertex 4 is 2 and outdegree is 1 HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Graph Representation : Two main data structures for the representation of graphs are used in practice. To store the graph in memory we require these type of representation technique. An adjacency List Representation An adjacency Matrix Representation HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs An adjacency List Representation An adjacency list is the representation of all edges or arcs in a graph as a list. If the graph is undirected, every entry is a set of two nodes containing the two ends of the corresponding edge; if it is directed, every entry is a tuple of two nodes, one denoting the source node and the other denoting the destination node of the corresponding arc. A B C D E F HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs An adjacency Matrix Representation The adjacency matrix uses a vector (one-dimensional array) for the vertices and a matrix (two-dimensional array) to store the edges. (b) Adjacency matrix for directed graph HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Graph Traversal: Graph traversal refers to the problem of visiting all the nodes in a graph in a particular manner. Tree traversal is a special case of graph traversal. In contrast to tree traversal, in general graph traversal, each node may have to be visited more than once, and a root-like node that connects to all other nodes might not exist. There are TWO types of traversal algorithms Breath First Search (BFS) Depth First Search (DFS) During the execution of our algorithm each node N of Graph G will be in one of three states, called the status of N, as follows:- STATUS = 1 [ Ready State ] The initial state of the node N. STATUS = 2 [ Waiting Sate] The node N is on the queue or stack. Waiting to be processed STATUS = 3 [ Processed State] The node N has been processed. HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Breath First Search (BFS) - Algorithm Step – I : Initially all nodes to be ready state (STATUS =1) Step – II: Put the starting node A in the QUEUE and change its status to the waiting state (STATUS=2) Step –III: Repeat Step IV and V until QUEUE is empty Step –IV: Remove the front node N of QUEUE. Process N and change the status of N to the processed state (STATUS = 3) Step –V: Add to the rear of QUEUE all the neighbour of N that are in the ready state (STATUS =1) and change their status to the waiting state (STATUS=2) [End of Step –III Loop] Step –VI: Exit HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Breath First Search (BFS) - Example HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Breath First Search (BFS) - Example (Contd.) HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Breath First Search (BFS) - Example BFS Sequence is: A B C D E F HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Depth First Search (DFS) - Algorithm Step – I : Initially all nodes to be ready state (STATUS =1) Step – II: Put the starting node A in the STACK and change its status to the waiting state (STATUS=2) Step –III: Repeat Step IV and V until STACK is empty Step –IV: Pop the top node N of STACK. Process N and change its status to the processed state (STATUS = 3) Step –V: Push into STACK all the neighbour of N that are still in ready state (STATUS =1) and change their status to the waiting state (STATUS=2) [End of Step –III Loop] Step –VI: Exit HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Depth First Search (DFS) - Example HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Depth First Search (DFS) - Example (Contd.) DFS Sequence is: A B C D E HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Depth First Search (DFS) Vs Breath First Search (BFS) HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Spanning Tree: A spanning tree of a graph is an undirected tree consisting of only those edges necessary to connect all the nodes in the original graph. For any pair of nodes there exists only one path between them and the insertion of any edge to a spanning tree forms a unique cycle. A minimum spanning tree or minimum weight spanning tree is then a spanning tree with weight less than or equal to the weight of every other spanning tree. There are two types of algorithms Kruskal‘s Algorithm Prim’s Algorithm HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Kruskal’s Algorithm: This algorithm finds a minimum spanning tree for a connected weighted graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. Kruskal's algorithm is an example of a greedy algorithm. Example: From the graph, draw a table with ascending order of weight. After that include one by one and reject the cyclic path. EdgeWeight Remarks AD5 CE5 DF6 AB7 BE7 BC8 X EF8 X BD9 X EG9 FG11 X DE15 X Cost: 39 HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Prim’s Algorithm: This algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. Example: This is our original weighted graph. The numbers near the arcs indicate their weight Vertex D has been arbitrarily chosen as a starting point. Vertices A, B, E and F are connected to D through a single edge. A is the vertex nearest to D and will be chosen as the second vertex along with the edge AD. HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Prim’s Algorithm (Example Contd.) The next vertex chosen is the vertex nearest to either D or A. B is 9 away from D and 7 away from A, E is 15, and F is 6. F is the smallest distance away, so we highlight the vertex F and the arc DF. The algorithm carries on as above. Vertex B, which is 7 away from A, is highlighted. In this case, we can choose between C, E, and G. C is 8 away from B, E is 7 away from B, and G is 11 away from F. E is nearest, so we highlight the vertex E and the arc EB. HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Graphs Prim’s Algorithm (Example) Here, the only vertices available are C and G. C is 5 away from E, and G is 9 away from E. C is chosen, so it is highlighted along with the arc EC. Vertex G is the only remaining vertex. It is 11 away from F, and 9 away from E. E is nearer, so we highlight it and the arc EG. Now all the vertices have been selected and the minimum spanning tree is shown in green. In this case, it has weight 39. HOME NEXT PREVIOUS

C o n f i d e n t i a l Developed By Nitendra Class summary Definition of Graph Type of Graphs Graph Representation An adjacency List Representation An adjacency Matrix Representation Graph Traversal Breath First Search (BFS) - Algorithm Depth First Search (DFS) - Algorithm Spanning Tree Kruskal’s Algorithm Prim’s Algorithm HOME PREVIOUS