– Graphs 1 Graph Categories Strong Components Example of Digraph

Slides:



Advertisements
Similar presentations
CSE Lectures 18 – Graphs Graphs & Characteristics
Advertisements

Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Data Structures Using C++
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Fundamentals of Python: From First Programs Through Data Structures
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
Graphs CS3240, L. grewe.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
1 Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp) Searching (DFS & BFS) Connected Components Graph G and Its Transpose.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Data Structures Using C++ 2E
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
Graphs. Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible.
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
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,
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
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.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
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++
Graphs Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Java Programming: Program Design Including Data Structures
Short paths and spanning trees
Graphs Graph transversals.
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

– Graphs 1 Graph Categories Strong Components Example of Digraph Main Index Contents – Graphs Graph Categories Example of Digraph Connectedness of Digraph Adjacency Matrix Adjacency Set vertexInfo Object Vertex Map and Vector vInfo VtxMap and Vinfo Example Breadth-First Search Algorithm (2 slides) Dfs() Strong Components Graph G and Its Transpose GT Shortest-Path Example (2 slides) Dijkstra Minimum-Path Algorithm (2 slides) Minimum Spanning Tree Example Minimum Spanning Tree: vertices A and B Completing the Minimum Spanning-Tree with Vertices C and D Summary Slides (4 slides)

Königsberg Bridge Problem Land River Land A and D are two islands in the river; a, b, c, d, e, f, and g are bridges. Question: Starting at one land area, is it possible to walk across all the bridges exactly once and return to the starting area?

Euler’s Model

Graph definitions and notations A graph G is a pair, G = (V, E), where V is a finite nonempty set of vertices of G and E  V x V., i.e., the elements of E are pair of elements of V and E is called edges. Let V(G) denote the set of vertices, and E(G) denote the set of edges of G. if elements of E(G) are ordered pairs, G is called directed graph or digraph; otherwise, G is called an undirected graph in which (u, v) and (v, u) represent the same edge. Let G be a graph, a graph H is called a subgraph of G if V(H)  V(G) and E(H)  E(G); i.e., every vertex of H is a vertex of G and every edge in H is an edge in G. A graph can be shown pictorially: a vertex is drawn as a circle, and an edge is drawn as a line. In a directed graph, the edges are drawn using arrows. Additional terms will be defined in the following slides.

5 Main Index Contents Graph Categories A graph is connected if each pair of vertices have a edge or path between them A complete graph is a connected graph in which each pair of vertices are linked by an edge

Graph with ordered edges are called directed graphs or digraphs 6 Main Index Contents Example of Digraph Graph with ordered edges are called directed graphs or digraphs

Connectedness of Digraph 7 Main Index Contents Connectedness of Digraph Strongly connected if there is a path from any vertex to any other vertex. Weakly connected if, for each pair of vertices vi and vj, there is either a path P(vi, vj) or a path P(vi,vj).

Representation of a graph: Adjacency Matrix 8 Main Index Contents Representation of a graph: Adjacency Matrix An m by m matrix (where m is the number of vertices in the graph), called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (vi,, vj). Its value is the weight of the edge, or –1 (or 0) if the edge does not exist.

Another way of Representing a graph: Adjacency list 9 Main Index Contents Another way of Representing a graph: Adjacency list

Definition of a node in a graph template <class vType) class nodeType { public: vType vertex; nodeType<vType> *link; };

Operations on a Graph Common operations Create a graph (demonstration based on adjacent list with a linked list) Clear the graph print a graph Determine if the graph is empty Traverse the graph Others (depending on applications)

Review: Linked list ADT template <class Type> class nodeType { public: type info; nodetype<Type> *link; }; Template<class Type> class linkedListType { const linkedListtype<Type> &operator=(const linkedListtype<Type> &); void initializedList( ); bool isEmpty( ); void print( ); int length( ); void destroyList( );

Review: Linked list ADT continued void retrieveFirst(Type &firstElement); void search(const Type &searchItem); // displys “Item is/is not found” void insertFirst(const Types &newItem); void insertLast(const Types &newItem); void deleteNode(const types &deleteItem); linkedListType( ); // initialize first = last = NULL linkedListType(const linkedListtype<type> &otherList); // copy constructor ~linkedListType( ); // delete all nodes in the list protedted: nodeType<Type> *first; nodeType<Type> *last; };

Review: Template // specifies an array data member with generic element // type elemType and its size with the following template template<class elemType, int size> class listType { public: … private: int maxSize; int length; elemType listelem[size]; }; To create a list of 100 int elements: listType<int, 100> inList;

Graph ADT: Adjacent list approach – the interface template<class vType, int size> class graphType { public: bool isEmpty( ); void createGraph( ); void clearGraph( ); // vertices deallocated void printGraph( ); graphtype( ); // default constructor; gsize = 0 and maxSize = size ~graphType( ); // storage deallocated protected: int maxSize; // max number of vertices int size; // current number of vertices linkedListGraph<vType> *graph; // array of pointers to create // the adjacency list };

Graph ADT: Adjacent list approach – the implementation template<class vType, int size> bool graphType<vtype, size>::isEmpty( ) { return (gsize == 0); } To implement the createGraph( ) funciton: assuming the user is prompted to enter at run time 1) number of vertices, 2) each and every vertex and its adjacent vertices, e.g., 5 0 2 4 … -99 1 0 3 … -99 2 … Note that –99 is the sentinel value used to terminal the input loop.

Graph ADT: Adjacent list approach – the implementation template<class vType, int size> bool graphType<vtype, size>::createGraph( ) { vtype vertex, adjacentVertex; if (gSize != 0) clearGraph( ); cout << “Enter # vertices followed by each individual vertices and “ << “its adjacent vertices ending with –99: ”; cin >> gSize; for (int i = 0; i < gSize; i++) { cin >> vertex; cin >> adjacentVertex; while (adjacentVertex != -99) { graph[vertex].insertLast(adjacentVertex); }

Graph ADT: Adjacent list approach – the implementation template<class vType, int size> bool graphType<vtype, size>::clearGraph( ) { int i; for (i = 0; i < gSize; i++) graph[i].destroyList( ); gSize = 0; }

The following slides for reference only

20 Main Index Contents vertexInfo Object A vertexInfo object consists of seven data members. The first two members, called vtxMapLoc and edges, identify the vertex in the map and its adjacency set.

Vertex Map and Vector vInfo 21 Main Index Contents Vertex Map and Vector vInfo To store the vertices in a graph, we provide a map<T,int> container, called vtxMap, where a vertex name is the key of type T. The int field of a map object is an index into a vector of vertexInfo objects, called vInfo. The size of the vector is initially the number of vertices in the graph, and there is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector

VtxMap and Vinfo Example

Breadth-First Search Algorithm

Breadth-First Search… (Cont.)

dfs()

Strong Components A strongly connected component of a graph G is a maximal set of vertices SC in G that are mutually accessible.

Graph G and Its Transpose GT The transpose has the same set of vertices V as graph G but a new edge set ET consisting of the edges of G but with the opposite direction.

Shortest-Path Example The shortest-path algorithm includes a queue that indirectly stores the vertices, using the corresponding vInfo index. Each iterative step removes a vertex from the queue and searches its adjacency set to locate all of the unvisited neighbors and add them to the queue.

Shortest-Path Example 29 Main Index Contents Shortest-Path Example Example: Find the shortest path for the previous graph from F to C.

Dijkstra Minimum-Path Algorithm From A to D Example 30 Main Index Contents Dijkstra Minimum-Path Algorithm From A to D Example

Dijkstra Minimum-Path Algorithm From… (Cont…)

Minimum Spanning Tree Example 32 Main Index Contents Minimum Spanning Tree Example

Minimum Spanning Tree: Vertices A and B 33 Main Index Contents Minimum Spanning Tree: Vertices A and B

Completing the Minimum Spanning-Tree Algorithm with Vertices C and D

§- Undirected and Directed Graph (digraph) 35 Main Index Contents Summary Slide 1 §- Undirected and Directed Graph (digraph) - Both types of graphs can be either weighted or nonweighted.

§- Breadth-First, bfs() 36 Main Index Contents Summary Slide 2 §- Breadth-First, bfs() - locates all vertices reachable from a starting vertex - can be used to find the minimum distance from a starting vertex to an ending vertex in a graph.

§- Depth-First search, dfs() 37 Main Index Contents Summary Slide 3 §- Depth-First search, dfs() - produces a list of all graph vertices in the reverse order of their finishing times. - supported by a recursive depth-first visit function, dfsVisit() - an algorithm can check to see whether a graph is acyclic (has no cycles) and can perform a topological sort of a directed acyclic graph (DAG) - forms the basis for an efficient algorithm that finds the strong components of a graph

§- Dijkstra's algorithm 38 Main Index Contents Summary Slide 4 §- Dijkstra's algorithm - if weights, uses a priority queue to determine a path from a starting to an ending vertex, of minimum weight - This idea can be extended to Prim's algorithm, which computes the minimum spanning tree in an undirected, connected graph.