CS212: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs CS3240, L. grewe.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Graphs – Part II CS 367 – Introduction to Data Structures.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Graphs Spring 2016CS202 - Fundamental Structures of Computer Science II1 Initially prepared by Dr. Ilyas Cicekli; improved by various Bilkent CS202 instructors.
Graphs A New Data Structure
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
CS202 - Fundamental Structures of Computer Science II
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
Data Structures 13th Week
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
Introduction to Graphs
Introduction to Graphs
Unit 3 Graphs.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs.
CS202 - Fundamental Structures of Computer Science II
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs Graph transversals.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CS223 Advanced Data Structures and Algorithms
Chapter 11 Graphs.
Graphs.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Spanning Trees Longin Jan Latecki Temple University based on slides by
ITEC 2620M Introduction to Data Structures
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
Graph Implementation.
Chapter 16 1 – Graphs Graph Categories Strong Components
Data Structures and Algorithm Analysis Graph Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
CMSC 341 Graphs.
Introduction to Graphs
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:

CS212: Data Structures and Algorithms Graphs

Outline Adjacency Matrix Adjacency list Linked list Depth First Graphs (Basic Concepts) Implementations of Graphs Adjacency Matrix Adjacency list Linked list Graph Traversals Depth First Breadth First

Graphs A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E, Each edge is a pair (v, w), where v, w  V Edges are sometimes referred to as arcs If the pair (V, W) is ordered, then the graph is directed (Diagraphs) Vertex w is adjacent to v, if and only if (v,w)  E For undirected graph, edge (v, w) or (w, v) => w is adjacent to v and v is adjacent to w Sometimes an edge (v, w), has a third component, (v, w, c), known as either a weight or a cost

Graphs A path in a graph is a sequence of vertices w1, w2, w3, …… wn such that (wi, wi+1)  E for 1<=i<n The length is the number of edges, equals to n-1 A path from a vertex to itself with length 0 Path(v,v) is referred to as a loop

Undirected Graphs Examples 1 2 3 4 (Connected) Vertices {1, 2, 3, 4} Edges {(1,2), (1, 3), (2, 3), (2, 4), (3, 4)} 1 2 3 4 Path = {(1, 2),(2,4), (4, 3)}

Undirected Graphs Examples 1 2 3 4 Tree (Graphs without Cycle) 1 2 3 4 Disconnected

Graphs A Simple Path: When all vertices are distinct, except that the first and the last could be the same A cycle in a directed graph is a path of length at least 1, such that w1 = wn This cycle is simple if the path is simple A path u, v, u in an undirected graph should not be considered as a cycle, because u, v and v, u are the same edge In a directed graph, these are different A directed graph is acyclic if it has no cycles (D.A.G)

Graphs An undirected graph is connected, if there is a path from every vertex to every other vertex A directed graph with this property is called strongly connected If a directed graph is not strongly connected, but the underlying graph (without directions) is connected, then the graph is weakly connected A complete graph is a graph in which there is an edge between every pair of vertices

Directed Graphs Examples Strongly Connected Directed Cycle Weakly Connected

Graphs Applications Airport System: Airport vertices and flights edges Examples of a real life situation that can be modeled by graphs are Airport System: Airport vertices and flights edges Traffic flow: Each street intersection represents a vertex, and each street is an edge

Implementations of Graphs Static Implementation Uses a two-dimensional array of integers Also called Adjacency Matrix Implementation Adjacency List Implementation Uses a one-dimensional array of pointers Linked List Implementation Uses a linked list of pointers Provides a complete dynamic implementation

1. Static Implementation of Graph 2 3 4 5 6 7 1 2 5 3 7 6 4

1. Static Implementation of Graph class SGraph { private: int noOfVertices; bool **aMatrix; public: SGraph(int nv) { //creates adjacency matrix to represent graph noOfVertices = nv; //Dynamically creates rows of two dimensional array aMatrix = new bool*[noOfVertices]; //Dynamically creates columns of two dimensional array for(int i = 0; i<noOfVertices; i++) aMatrix[i] = new bool[noOfVertices]; } Class continues on next slide…

1. Static Implementation of Graph void insertGraph() //inserts graph in adjacency matrix { char choice; for(int i = 0; i<noOfVertices; i++) for(int j = 0; j<noOfVertices; j++) { cout<<"is there an edge b/w vertex"<<i<<"and"<<j; cin>>choice; if(choice == 'y' || choice == 'Y') aMatrix[i][j] = 1; else aMatrix[i][j] = 0; } };

2. Adjacency List Implementation For each vertex keep a list of all adjacent vertices 1 2 3 4 5 6 7 1 2 5 3 7 6 4

2. Adjacency List Implementation class DGraph { private: int noOfVertices; SinglyLinkedList<int> *aList; public: //creates adjacency list to represent graph DGraph(int nv) noOfVertices = nv; aList = new SinglyLinkedList<int>[noOfVertices]; } Class continues on next slide…

2. Adjacency List Implementation //inserts graph in adjacency list void insertGraph() { for(int i = 0; i<noOfVertices; i++) { int vertexNo; cout<<"Enter a number of vertex adjacent to vertex" <<i<<" Enter -1 to terminate"; cin>>vertexNo; while(vertexNo != -1) { aList[i].addToTail(vertexNo); } };

Implementations of Graphs In most real life situations, the vertices have names, which are known at compile time, instead of numbers We have to provide a mapping of names to numbers using a hash table

Graph Traversals Traversing a graph consists of visiting each vertex only once We can traverse the graph by two ways: Depth first Breadth first

Depth First Graph Traversal Depth-first search is a generalization of preorder traversal for trees It is basically a Last visited, First explored strategy Let V be the last vertex visited and N1, N2, …, Nk are adjacent to V A depth-first traversal will: visit N1, then proceed to traverse all the unvisited successors of N1, then proceed to traverse the remaining adjacent vertices of V in similar fashion

Depth First Graph Traversal Declare an array visited of type bool and initialize it to false Each index of this array indicates weather a particular vertex is visited or not

Depth First Graph Traversal //Algorithm for Depth First Traversal void depthFirstTraversal(int TotalVertices) { bool *visited = new int[TotalVertices]; for(int i = 0; i < TotalVertices; i++) visited[i] = false; while there is a vertex v which is not visited DFT(v, visited) } Algorithm Continues on next slide…

Depth First Graph Traversal //Algorithm for DFT void DFT(Vertex v, bool visited[ ]) { Stack s; s.push(v); while(!s.isempty()) { v = s.pop(); if(visited[v] == false) { visited[v] = true; for each vertex u adjacent to v if(visited[u] == false) s.push(u); }

Breadth First Graph Traversal It is basically a level order traversal Let V be the last node visited and N1, N2, …, Nk are adjacent to V A breadth-first traversal will visit N1, then N2, and so forth through Nk, then proceed to traverse all the unvisited vertices adjacent to N1, then traverse the unvisited vertices adjacent to N2,…Nk in similar fashion

Breadth First Graph Traversal //Algorithm for Breadth First Traversal breadthFirstTraversal(Vertex v) { Add V to Queue & mark it While (Queue not Empty) { Remove Vertex from Queue & call it w for (each unvisited vertex u adjacent to w) mark u visited add u in Queue }

Breadth First Graph Traversal C D B E F G I H

Breadth First Graph Traversal C Queue B A C D B E F G I H

Breadth First Graph Traversal Dqueue C B A C D B E F G I H

Breadth First Graph Traversal Enqueu D D A C D B E F G I H

Breadth First Graph Traversal Dqueue B D A C D B E F G I H

Breadth First Graph Traversal Enqueue E, I, H E I H A C D B E F G I H

Breadth First Graph Traversal Dqueue D E I H A C B D E I H F G

Breadth First Graph Traversal Dqueue E I H A C B D E I H F G

Breadth First Graph Traversal Enqueue F, G H F G A C D B E F G I H

Breadth First Graph Traversal Dqueue I H F G A C D B E F G I H

Breadth First Graph Traversal Dqueue I H F G A C D B E F G I H H already in queue i.e. visited

Breadth First Graph Traversal Dqueue H F G A C B D E I H F G

Breadth First Graph Traversal Dqueue F G A C B D E I H F G

Breadth First Graph Traversal Dqueue G A C B D E I H F G