CSE Lectures 18 – Graphs Graphs & Characteristics

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Unit-iv.
CSE 211 Discrete Mathematics
ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures.
CSE 373 Data Structures and Algorithms Lecture 20: Graphs II.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
CSE Lecture 12 – Linked Lists …
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
Graphs, representation, isomorphism, connectivity
CS1022 Computer Programming & Principles
CS203 Lecture 15.
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.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
© 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.
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.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Using nextAdjacent() To list all vertices connected to a vertex u in graph G we can use the following loop: for (int v=G.nextAdjacent(u,-1); v>=0; v=G.nextAdjacent(u,v))
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 261 – Data Structures Graphs. Used in a variety of applications and algorithms Graphs represent relationships or connections Superset of trees (i.e.,
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
1 Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp) Searching (DFS & BFS) Connected Components Graph G and Its Transpose.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Search Related Algorithms. Graph Code Adjacency List Representation:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Graph. Terminologi Graph A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices. – An edge e = (vi,vj) connects.
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.
1 Data Structures for Graphs Edge list Adjacency lists Adjacency matrix.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Chapter 05 Introduction to Graph And Search Algorithms.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Podcast Ch26a Title: Representing Graphs
Introduction to Graphs
Data Structures and Algorithms for Information Processing
Graph Operations And Representation
Graphs Graph transversals.
Graphs Representation, BFS, DFS
Graphs Chapter 11 Objectives Upon completion you will be able to:
Podcast Ch25d Title: Minimum Path Algorithm
Graphs.
Graph Implementation.
Chapter 16 1 – Graphs Graph Categories Strong Components
Podcast Ch26b Title: Digraph Class Implementation
Introduction to Graphs
Presentation transcript:

CSE 30331 Lectures 18 – Graphs Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp)

What is a graph? Formally, a graph G(V,E) is A set of vertices V A set of edges E, such that each edge ei,j connects two vertices vi and vj in V V and E may be empty

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

Example of Digraph Graphs with ordered edges are called directed graphs or digraphs

Strength of Connectedness (digraphs only) Strongly connected if there is a path from each vertex to every other vertex. C A E D B (b) Strongly Connected A C B E D (a) Not Strongly or Weakly Connected (No path to D or D to )

Strength of Connectedness (digraphs only) 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). A C B E D (a) ( c) Not Strongly or Weakly Connected (No path to D or D to ) Weakly Connected (No path from D to a vertex)

Representing Graphs Adjacency Matrix Adjacency Set (or Adjacency List) Edges are represented in a 2-D matrix Adjacency Set (or Adjacency List) Each vertex has an associated set or list of edges leaving Edge List The entire edge set for the graph is in one list Mentioned in discrete math (probably)

Adjacency Matrix An m x m matrix, called an adjacency matrix, identifies graph 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 if the edge does not exist. D A C E B

Adjacency Set (or List) An adjacency set or adjacency list represents the edges in a graph by using … An m element map or vector of vertices Where each vertex has a set or list of neighbors Each neighbor is at the end of an out edge with a given weight (a) D A C E B Vertices Set of Neighbors 1

Adjacency Matrix and Adjacency Set (side-by-side) 4 2 7 3 6 1 A E D C B Vertices Set of Neighbors 4 2 7 3 6 1

Building a graph class Neighbor VertexInfo VertexMap Identifies adjacent vertex and edge weight VertexInfo Contains all characteristics of a given vertex, either directly or through links VertexMap Contains names of vertices and links to the associated VertexInfo objects

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.

vertexInfo object vtxMapLoc – iterator to vertex (name) in map edges – set of vInfo index / edge weight pairs Each is an OUT edge to an adjacent vertex vInfo[index] is vertexInfo object for adjacent vertex inDegree – # of edges coming into this vertex outDegree is simply edges.size() occupied – true (this vertex is in the graph), false (this vertex was removed from graph) color – (white, gray, black) status of vertex during search dataValue – value computed during search (distance from start, etc) parent – parent vertex in tree generated by search

A Neighbor class (edges to adjacent vertices) class neighbor { public: int dest; // index of destination vertex in vInfo vector int weight; // weight of this edge // constructor neighbor(int d=0, int c=0) : dest(d), weight(c) {} // operators to compare destination vertices friend bool operator<(const neighbor& lhs, const neighbor& rhs) { return lhs.dest < rhs.dest; } friend bool operator==(const neighbor& lhs, const neighbor& rhs) return lhs.dest == rhs.dest; };

vertexInfo object (items in vInfo vector) template <typename T> class vertexInfo { public: enum vertexColor { WHITE, GRAY, BLACK }; map<T,int>::iterator vtxMapLoc; // to pair<T,int> in map set<neighbor> edges; // edges to adjacent vertices int inDegree; // # of edges coming into vertex bool occupied; // currently used by vertex or not vertexColor color; // vertex status during search int dataValue; // relevant data values during search int parent; // parent in tree built by search // default constructor vertexInfo(): inDegree(0), occupied(true) {} // constructor with iterator pointing to vertex in map vertexInfo(map<T,int>::iterator iter) : vtxMapLoc(iter), inDegree(0), occupied(true) };

A graph using a vertexMap and vertexInfo vector Graph vertices are stored in a map<T,int>, called vtxMap Each entry is a <vertex name, vertexInfo index> key, value pair The initial size of the vertexInfo vector is the number of vertices in the graph There is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector vertex mIter (iterator location) index . . . vtxMapLoc edges inDegree occupied color dataValue vtxMap parent vInfo

A graph class (just the private members) typedef map<T,int> vertexMap; vertexMap vtxMap; // store vertex in a map with its name as the key // and the index of the corresponding vertexInfo // object in the vInfo vector as the value vector<vertexInfo<T> > vInfo; // list of vertexInfo objects for the vertices int numVertices; int numEdges; // current size (vertices and edges) of the graph stack<int> availStack; // availability stack, stores unused vInfo indices

VtxMap and Vinfo Example C B

Find the location for vertexInfo of vertex with name v // uses vtxMap to obtain the index of v in vInfo. // this is a private helper function template <typename T> int graph<T>::getvInfoIndex(const T& v) const { vertexMap::const_iterator iter; int pos; // find the vertex : the map entry with key v iter = vtxMap.find(v); if (iter == vtxMap.end()) pos = -1; // wasn’t in the map else pos = (*iter).second; // the index into vInfo return pos; }

Find in and out degree of v // return the number of edges entering v template <typename T> int graph<T>::inDegree(const T& v) const { int pos=getvInfoIndex(v); if (pos != -1) return vInfo[pos].inDegree; else throw graphError("graph inDegree(): v not in the graph"); } // return the number of edges leaving v int graph<T>::outDegree(const T& v) const return vInfo[pos].edges.size(); throw graphError("graph outDegree(): v not in the graph");

Insert a vertex into graph template <typename T> void graph<T>::insertVertex(const T& v) { int index; // attempt insertion, set vInfo index to 0 for now pair<vertexMap::iterator, bool> result = vtxMap.insert(vertexMap::value_type(v,0)); if (result.second) { // insertion into map succeeded if (!availStack.empty()) { // there is an available index index = availStack.top(); availStack.pop(); vInfo[index] = vertexInfo<T>(result.first); } else { // vInfo is full, increase its size vInfo.push_back(vertexInfo<T>(result.first)); index = numVertices; } (*result.first).second = index; // set map value to index numVertices++; // update size info else throw graphError("graph insertVertex(): v in graph");

Insert an edge into graph // add the edge (v1,v2) with specified weight to the graph template <typename T> void graph<T>::insertEdge(const T& v1, const T& v2, int w) { int pos1=getvInfoIndex(v1), pos2=getvInfoIndex(v2); if (pos1 == -1 || pos2 == -1) throw graphError("graph insertEdge(): v not in the graph"); else if (pos1 == pos2) throw graphError("graph insertEdge(): loops not allowed"); // insert edge (pos2,w) into edge set of vertex pos1 pair<set<neighbor>::iterator, bool> result = vInfo[pos1].edges.insert(neighbor(pos2,w)); if (result.second) // it wasn’t already there // update counts numEdges++; vInfo[pos2].inDegree++; }

Erase an edge from graph // erase edge (v1,v2) from the graph template <typename T> void graph<T>::eraseEdge(const T& v1, const T& v2) { int pos1=getvInfoIndex(v1), pos2=getvInfoIndex(v2); if (pos1 == -1 || pos2 == -1) throw graphError("graph eraseEdge(): v not in the graph"); // find the edge to pos2 in the list of pos1 neighbors set<neighbor>::iterator setIter; setIter = vInfo[pos1].edges.find(neighbor(pos2)); if (setIter != edgeSet.end()) // found edge in set, so remove it & update counts vInfo[pos1].edges.erase(setIter); vInfo[pos2].inDegree--; numEdges--; } else throw graphError("graph eraseEdge(): edge not in graph");

Erase a vertex from graph (algorithm) Find index of vertex v in vInfo vector Remove vertex v from map Set vInfo[index].occupied to false Push index onto availableStack For every occupied vertex in vInfo Scan neighbor set for edge pointing back to v If edge found, erase it For each neighbor of v, decrease its inDegree by 1 Erase the edge set for vInfo[index]