E.G.M. PetrakisGraphs1 Graph G(V,E): finite set V of nodes (vertices) plus a finite set E of arcs (or edges) between nodes –V = {A, B, C, D, E, F, G} –E.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

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.
Graph Searching CSE 373 Data Structures Lecture 20.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Graph & BFS.
1 Graphs: shortest paths & Minimum Spanning Tree(MST) Fundamental Data Structures and Algorithms Ananda Guna April 8, 2003.
E.G.M. PetrakisGraphs1  Graph G(V,E): finite set V of nodes (vertices) plus a finite set E of arcs (or edges) between nodes  V = {A, B, C, D, E, F, G}
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.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS Data Structures Chapter 6 Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
IS 2610: Data Structures Graph April 5, 2004.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
UNCA CSCI November, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
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.
Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
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
E.G.M. PetrakisGraphs1  Graph G(V,E): finite set V of nodes (vertices) plus a finite set E of arcs (or edges) between nodes  V = {A, B, C, D, E, F, G}
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,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
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. 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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
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.
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.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
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.
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 2015, Fall Pusan National University Ki-Joune Li.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
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.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures 13th Week
COMP108 Algorithmic Foundations Greedy methods
Minimum Spanning Trees and Shortest Paths
Introduction to Graphs
CSE 373 Data Structures and Algorithms
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
CMSC 341 Lecture 20.
2017, Fall Pusan National University Ki-Joune Li
GRAPHS G=<V,E> Adjacent vertices Undirected graph
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:

E.G.M. PetrakisGraphs1 Graph G(V,E): finite set V of nodes (vertices) plus a finite set E of arcs (or edges) between nodes –V = {A, B, C, D, E, F, G} –E = {AA, AB, AD, CD, DE, BF, EF, FG, EG } A B G C E F D

E.G.M. PetrakisGraphs2 Terminology Two vertices are adjacent if they are connected with an edge –Neighbor vertices Subgraph of G=(V,E): a graph G’=(V’,E’) where V’, E’ are subsets of V, E |V|, |E|: number of nodes and edges of G Complete graph: contains all possible edges – |E| in Θ(|V| 2 )

E.G.M. PetrakisGraphs3 G E H A B C F D Undirected graphs: the edges are not directed from one vertex to another

E.G.M. PetrakisGraphs4 Directed Graphs: the edges are directed G E H A B C F D

E.G.M. PetrakisGraphs5 G E A BC FD A D BC

E.G.M. PetrakisGraphs6 Vertex degree : maximum number of incoming or outgoing edges. –In-degree: number of incoming edges –Out-degree: number outgoing edges Relation R on A: R = { | x,y ∊ N} –x < y –x mod y = odd Labeled graph : labels are associated to vertices or edges Weighted graph: weights on edges

E.G.M. PetrakisGraphs Relation Weighted

E.G.M. PetrakisGraphs8 Vertices (V 1,V 2,…V n ) form a path if there exist edges from V i to V i+1 for 1<= i <= n –V 1,V 2,…V n : adjacent –Length of path : number of edges –Weighted graphs: sum of weights on path –Cycle: path (of length 3 or more) connecting a vertex with itself – Cyclic graph : contains cycle – Acyclic graph : does not contain cycle – DAG: Directed Acyclic Graph

E.G.M. PetrakisGraphs9 An undirected graph is connected if there is at least one path between any two vertices Connected components: maximally connected subgraphs Free tree of graph: connected, undirected, acyclic subgraph of the graph –A free tree has |V| nodes and |V|-1 edges

E.G.M. PetrakisGraphs10 join (a,b) join (a,b,x) remv[wt] (a,b,x) adjacent (a,b) ab ab ab ab x ab ab x a b c adjacent (a,b) = true adjacent (a,c) = false Operations on graphs

E.G.M. PetrakisGraphs11 Build Graphs read(n);// number of nodes read and create n nodes label them from 1.. n While not eof(..) { read(node 1, node 2, w 12 ); joinwt(node 1, node 2, w 12 ); } any graph algorithm

E.G.M. PetrakisGraphs12 Representation of Directed Graphs Directed Graph adjacency matrix adjacency list

E.G.M. PetrakisGraphs13 Representation of Undirected Graphs Undirected Graph Adjacency List Adjacency Matrix

E.G.M. PetrakisGraphs14 Graph Implementation Matrix representation, Adjacency list –simple implementation –difficult to process edges –unused space in matrix auxiliary functions and structures –Mark: 1D array marking vertices Mark[i] = 1 if vertex i has been processed (visited) –typedef int * Edge; –matrix: the actual Graph implemented as an 1D array of size n 2 edge (i, j) is found at matrix[i * n + j] if edge exists matrix[i * n + j] = wt otherwise matrix[i * n + j] = NOEDGE

E.G.M. PetrakisGraphs15 Class Graph {// Graph class: Adjacency matrix private: int* matrix;//The edge matrix int numVertex;//Number of vertices int numEdge;//Number of edges bool* Mark;//The mark array public: Graph( );//Constructor ~Graph( );//Destructor int n( );//Number of vertices for graph int e( );//Number of edges for graph Edge first(int);//Get the first edge for a vertex bool isEdge(Edge);//TRUE if this is an edge Edge next(Edge);//Get next edge for a vertex int v1(Edge);//Return vertex edge comes from int v2(Edge);//Return vertex edge goes to int weight(int, int);//Return weight of edge int weight(Edge);//Return weight of edge }

E.G.M. PetrakisGraphs16 Graph::Graph( ) {//Constructor Mark = NULL; matrix = NULL;//plus code for creating graph } Graph::~Graph( ) {//Destructor: return allocated space if (Mark != NULL) delete [ ] Mark; if (matrix != NULL) delete [ ] matrix; } int Graph::n( ) { return numVertex; } //Num. of vertices for the graph int Graph::e( ) //Number of edges { return numEdge; } Edge Graph::first(int v) {//Get the first edge for a vertex int stop = (v+1) * numVertex;//Position at end of v’s row for ( int pos = v * numVertex; pos < stop; pos++) //scans entire row if ( matrix[pos] != NOEDGE) return &matrix[pos]; return NULL; }

E.G.M. PetrakisGraphs17 bool Graph::isEdge(Edge w)//TRUE if (w,0) is an edge { return w != NULL; } Edge Graph::next(Edge w) {//Get next edge of (w,0) int stop = (v1(w) + 1) * numVertex;//Position at end of row for (int pos = (w – matrix) + 1; pos < stop; pos++) if (matrix[pos] != NOEDGE)return &matrix[pos]; return NULL; } int Graph::v1(Edge w)//Return 1 st vertex of edge { return ( w – matrix ) / numVertex; }//Pointer arithmetic int Graph::v2(Edge w)//Return 2 nd vertex edge { return ( w – matrix ) % numVertex; }//Pointer arithmetic int Graph::weight( int i, int j) {//Return weight of edge if ( matrix[ i * numVertex + j ] = = NOEDGE ) return INFINITY; else return matrix[ i * numVertex + j ]; } int Graph::weight(Edge w) {//Return weight of edge if ( * w = = NOEDGE ) return INFINITY; else return * w; }

E.G.M. PetrakisGraphs18 Class Graph {// Graph class: Adjacency list private: Edge * list;//The vertex list int numVertex;//Number of vertices int numEdge;//Number of edges bool * Mark;//The mark array public: Graph( );//Constructor ~Graph( );//Destructor int n( );//Number of vertices for graph int e( );//Number of edges for graph Edge first(int);//Get the first edge for a Vertex bool isEdge(Edge);//TRUE if this is an edge Edge next(Edge);//Get next edge for a vertex int v1(Edge);//Return vertex edge comes from int v2(Edge);//Return vertex edge goes to int weight(int, int);//Return weight of edge int weight(weight);//Return weight of edge };

E.G.M. PetrakisGraphs19 class EdgeLink {//A singly-linked list node public: int weight;//Edge weight int v1;//1 st vertex of edge int v2; //2 nd vertex of edge EdgeLink * next;//Pointer to next edge in list EdgeLink(int vt1, int vt2, int w, EdgeLink * nxt = NULL) //Constructor { v1 = vt; v2 = vt2; weight = w; next = nxt; } }; Typedef EdgeLink* Edge;

E.G.M. PetrakisGraphs20 Graph::Graph( ) {//Constructor Mark = NULL;list = NULL;// the code for creating a graph follows } Graph::~Graph( ) {//Destructor: return allocated space if (Mark != NULL) delete [ ] Mark; if (list != NULL) {//Remove all of the edges for (int v = 0; v < n( ); v++)//For each vertex while (list[v] != NULL) {//return its edges Edge temp = list[v]; list[v]= list[v]  next; delete temp; } delete [ ] list;//Now remove the vertex list headers } int Graph::n( ) { return numVertex; }//Number of vertices int Graph::e( ) { return numEdge; }//Number of edges

E.G.M. PetrakisGraphs21 Edge Graph::first(int v)//Get the first edge for a vertex {return list[v]; } bool Graph::isEdge(Edge w)//TRUE if this is an edge { return w != NULL; } Edge Graph::next(Edge w) {//Get next edge for a vertex if (w == NULL) return NULL; else return w  next; } int Graph::v1(Edge w)//Return vertex edge comes from { return w  v1; } int Graph::v2(Edge w)//Return vertex edge goes to { return w  v2; }

E.G.M. PetrakisGraphs22 int Graph::weight( int i, int j) {//Return weight of edge for (Edge curr = list[i]; curr != NULL; curr = curr ⇢ next) if (curr  v2 = = j ) return curr  weight; return INFINITY; } int Graph::weight(Edge w) {//Return weight of edge if ( w = = NULL ) return INFINITY; else return w  weight; }

E.G.M. PetrakisGraphs23 arcptrinfonextnode info: data arcptr: pointer to an adjacent node nextnode: pointer to a graph node ndptrnextarc ndptr: pointer to adjacent node nextarc: pointer to next edge E A B C D An even better representation

E.G.M. PetrakisGraphs24 AΒ nilnil CDE nilnil nilnil nilnil nilnil nilnil graph

E.G.M. PetrakisGraphs25 Trees –preorder –inorder –postorder Graphs –Depth first search –Breadth first search Graph Traversals

E.G.M. PetrakisGraphs26 Depth First Search: –Starting from vertex v, initially all vertices unvisited void DFS(v) { Mark(v) = true; for each vertex w adjacent to v if (!Mark(w)) DFS(w) } Complexity DFS : –O(|V| + |E|): adjacency list representation O(|V| 2 ) in dense graphs –O(|V| 2 ): matrix representation

E.G.M. PetrakisGraphs27 DFS : V 1 V 2 V 4 V 8 V 5 V 6 V 3 V 7 v = v 1 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8 v3v3 v2v2 v1v1 v6v6 v5v5 v4v4 v8v8 v7v

E.G.M. PetrakisGraphs28 Breadth-First Search ΒFS : V 1 V 2 V 3 V 4 V 5 V 6 V 7 V 8 v = v 1 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8

E.G.M. PetrakisGraphs29 Breadth First Search (BFS): –Starting from vertex |V|, Initially all nodes are “unvisited” –Visit adjacent nodes of current nodes (use a queue) void DFS(v) { visited(v) = true; enqueue(v, Q); while ( Q ≠ 0 ) { x = dequeue(Q); for each y adjacent x do if ! Mark(y) { Mark(y) = TRUE; enqueue(y,Q); }

E.G.M. PetrakisGraphs30 v1v1 front rear v3v3 v2v2 front rear output(v 1 ) v3v3 front rear v5v5 v4v4 output(v 2 ) output(v 3 ) v4v4 front rear v7v7 v5v5 v6v6 v5v5 front rear v8v8 v6v6 v7v7 output(v 4 ) v6v6 front v7v7 v8v8 output(v 5 ) v6v6 front v8v8 output(v 6 ) output(v 7 ) v = v 1 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v8v8

E.G.M. PetrakisGraphs31 Complexity of BFS: –O(|V|+|E|) : adjacency list representation –d 1 + d d n = |E| –d i = degree (v i ) –O(|V| 2 ) : adjacency matrix representation

E.G.M. PetrakisGraphs32 void DFS (Graph& G, int v) {// Depth-First Search action(G,v); G.Mark[v] = VISITED; for ( Edge w = G.first(v); G.isEdge(w); w = G.next(w)) if (G.Mark[G.v 2 (w)] = = UNVISITED) DFS ( G, G.v 2 (w)); action(G,v); } void BFS (Graph& G, int start) {// Breadth-First Search Queue Q(G.n( )); Q.enqueue(start); G.Mark[start] = VISITED; While ( !Q.isempty( )){ int v = Q.dequeue( ); action(G,v); for ( Edge w = G.first(v); G.isEdge(w); w = G.next(w)) if (G.Mark[G.v 2 (w)] = = UNVISITED) { G.Mark[G.v 2 (w)] = VISITED; Q.enqueue(G.v 2 (w)); } action(G,v); }

E.G.M. PetrakisGraphs33 connected graphs: undirected graph, there is a path connecting any two nodes unconnected graphs: not always a path A BC D E connected components E F G A B C D

E.G.M. PetrakisGraphs34 If there exist unconnected nodes in a DFS or BFS traversal of G then G is unconnected Find all connected components: void COMP(G, n) { for i = 1 to n if Mark(i) == UNVISITED then DFS(i) [or BFS(i)]; } Complexity: O(|V| + |E|)

E.G.M. PetrakisGraphs35 Tree formed from edges and nodes of G Spanning Trees Spanning Trees of G : (G,E)

E.G.M. PetrakisGraphs36 Spanning forest of G = ( V, E ): set of disjoint spanning trees of G –T i = ( V i, E i ) 1≤ i ≤ k where V i and E i are subsets of V and E DFS produces depth first spanning trees or forest BFS breadth first spanning trees / forest Undirected graphs provide more traversals –produce less but short spanning trees Directed graphs provide less traversals –produce more and higher spanning trees

E.G.M. PetrakisGraphs37 DFS DFS spanning tree D BC E A DFS C B DE A C BF E A G D D C EGF B A DFS spanning forest

E.G.M. PetrakisGraphs38 BFS BFS spanning tree ΒFS E BC F A D GE BC F A D G BFS spanning forest C BF E A H D C H DE F B A ΒFS

E.G.M. PetrakisGraphs39 Minimum Cost Spanning Tree cost T = Prim’s algorithm: Complexity O(|V| 2 )

E.G.M. PetrakisGraphs40 void Prim(Graph& G, int s) {//Prim’s MST algorithm int D[G.n( )];//Distance vertex int V[G.n( )];//Who’s closest for (int i=0; i<G.n( ); i++); D[i] = INFINITY; //Initialize D[s] = 0; for ( i=0; i<G.n( ); i++){//Process the vertices int v = minVertex(G, D); G.Mark[v] = VISITED; if ( v != s ) AddEdgetoMST(V[v], v);//Add this edge to MST if (D[v] = = INFINITY ) return;//unreachable vertices for ( Edge w = G.first(v); G.isEdge(w); w = G.next(w)) if (D[G.v2(w)] > G.weight(w)) { D[G.v2(w)] = G.weight(w);//Update distance and V[G.v2(w)] = v;// who it came from }

E.G.M. PetrakisGraphs41 void minVertex(Graph& G, int* D) {//Find min cost vertex int v; //Initialize v to any unvisited vertex for (int i = 0; i < G.n( ); i++)//Initialize if (G.Mark[i] = = UNVISITED){ v = i; break; } for ( i=0; i<G.n( ); i++)//Now find smallest value if ((G.Mark[i] = = UNVISITED) && ( D[i] < D[v] )) v = i; return v; }

E.G.M. PetrakisGraphs42 Dijkstra’s algorithm: find the shortest path from a given node to every other node in a graph G –No better algorithm for single ending node –Notation: G = (V,E) : input graph C[i,j] : distance between nodes i, j V : starting node S : set of nodes for which the shortest path from v has been computed D(W) : length of shortest path from v to w passing through nodes in S

E.G.M. PetrakisGraphs starting point: v = 1 stepSWD(2)D(3)D(4)D(5) 1{1}-10 infinite {1,2} {1,2,4} {1,2,4,3} {1,2,4,3,5}

E.G.M. PetrakisGraphs44 function Dijkstra(G: graph, int v) { S = {1}; for i = 2 to n D[i] = C[i,j]; while (S != V) { choose w from V-S: D[w] = minimum S = S + {w}; for each v in V–S: D[v] = min{D[v], D[w]+[w,v]}*; } * If D[w]+C[w,v] < D[v] then P[v] = w: keep path in array

E.G.M. PetrakisGraphs45 void Dijkstra(Graph& G, int s) { // Compute shortest path distances int D[G.n( )]; for (int i=0; i < G.n( ); i++) // Initialize D[i] = INFINITY; D[s] = 0; for (i = 0; i < G.n( ); i++) { // Process the vertices int v = minVertex(G, D); if (D[v] == INFINITY) return; // Remaining vertices unreachable G.setMark(v, VISITED); for (Edge w = G.first(v); G.isEdge(w); w = G.next(w)) if (D[G.v2(w)] > (D[v] + G.weight(w))) D[G.v2(w)] = D[v] + G.weight(w); }

E.G.M. PetrakisGraphs46 int minVertex(Graph& G, int* D) { // Find min cost vertex int v; // Initialize v to any unvisited vertex; for (int i = 0; i < G.n( ); i++) if (G.getMark(i) == UNVISITED) { v = i; break; } for (i++; i < G.n( ); i++) // Now find smallest D value if ((G.getMark(i) == UNVISITED) && (D[i] < D[v])) v = i; return v; } minVertex: scans through the list of vertices searching for the min value  O(|V|) time Complexity : O(|V| 2 + |E|) = O(|V| 2 ) since O(|E|) is O(|V| 2 )