IS 2610: Data Structures Graph April 5, 2004.

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

Lecture 15. Graph Algorithms
22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Structures Using C++
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
16-Graphs Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS Data Structures Chapter 6 Graphs.
Graphs. Graph A “graph” is a collection of “nodes” that are connected to each other Graph Theory: This novel way of solving problems was invented by a.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Social Media Mining Graph Essentials.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
GRAPH Learning Outcomes Students should be able to:
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
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.
Graph Search Computing 2 COMP s1. P ROBLEMS ON G RAPHS What kinds of problems do we want to solve on/via graphs? Is there a simple path from A to.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
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.
1 CS104 : Discrete Structures Chapter V Graph Theory.
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.
Data Structures & Algorithms Graphs
Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
Unit – V Graph theory. Representation of Graphs Graph G (V, E,  ) V Set of vertices ESet of edges  Function that assigns vertices {v, w} to each edge.
Graph Theory and Applications
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.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Introduction to Graph Theory Lecture 17: Graph Searching Algorithms.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs Upon completion you will be able to:
Graphs 2015, Fall Pusan National University Ki-Joune Li.
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.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Breadth-First Search (BFS)
Graphs Chapter 20.
Chapter 5 : Trees.
Graph theory Definitions Trees, cycles, directed graphs.
C.Eng 213 Data Structures Graphs Fall Section 3.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CS120 Graphs.
Graph Algorithms Using Depth First Search
Graph Algorithm.
Graph Algorithm.
Graphs Chapter 13.
2018, Fall Pusan National University Ki-Joune Li
Chapter 11 Graphs.
2017, Fall Pusan National University Ki-Joune Li
CSCI2100 Data Structures Tutorial
Text Book: Introduction to algorithms By C L R S
Presentation transcript:

IS 2610: Data Structures Graph April 5, 2004

Graph Terminology Graph : vertices + edges Induced subgraph of a subset of vertices Connected graph: a path between every pair Maximal connected: there is no path from this subgraph to an outside vertex

Representation Adjacency matrix Adjacency list V by v array typedef struct { int v; int w; } Edge; Edge EDGE(int, int); typedef struct graph *Graph; Graph GRAPHinit(int); void GRAPHinsertE(Graph, Edge); void GRAPHremoveE(Graph, Edge); int GRAPHedges(Edge [], Graph G); Graph GRAPHcopy(Graph); void GRAPHdestroy(Graph); Adjacency matrix V by v array Adv./disadvantages Adjacency list Linked list for each vertex typedef struct node *link; struct node { int v; link next; }; struct graph { int V; int E; link *adj; }; Graph GRAPHinit(int V) { int v; Graph G = malloc(sizeof *G); G->V = V; G->E = 0; G->adj = malloc(V*sizeof(link)); for (v = 0; v < V; v++) G->adj[v] = NULL; return G; }

Hamilton Path Hamilton path: Given two vertices, is there a simple path connecting them that visits every vertex in the graph exactly once? Worst case for finding Hamilton tour is exponential Assume one vertex isolated; and all v-1 vertices are connected (v-1)! Edges need to be checked

Euler Tour/Path Euler Path Is there a path connecting two vertices that uses each edge in the graph exactly once? Vertices may be visited multiple times Euler tour: Is there a cycle with each edges exactly once Bridges of konigsberg Properties: A graph has a Euler tour iff it is connected and all the vertices are of even degree A graph has a Euler path iff it is connected and exactly two of its vertices are of odd degrees Complexity?

Graph Search Depth First Search V2 for adj matrix V+E for adj. list #define dfsR search void dfsR(Graph G, Edge e) { int t, w = e.w; pre[w] = cnt++; for (t = 0; t < G->V; t++) if (G->adj[w][t] != 0) if (pre[t] == -1) dfsR(G, EDGE(w, t)); } Depth First Search V2 for adj matrix V+E for adj. list Graphs may not be connected void dfsR(Graph G, Edge e) { link t; int w = e.w; pre[w] = cnt++; for (t = G->adj[w]; t != NULL; t = t->next) if (pre[t->v] == -1) dfsR(G, EDGE(w, t->v)); } static int cnt, pre[maxV]; void GRAPHsearch(Graph G) { int v; cnt = 0; for (v = 0; v < G->V; v++) pre[v] = -1; for (v = 0; v < G->V; v++) if (pre[v] == -1) search(G, EDGE(v, v)); }

DFS for graph problems Cycle detection Simple path Simple connectivity Back edges Simple path Simple connectivity The graph search function calls the recursive DFS function only once. Two way Euler tour Each edge visited exactly twice Spanning tree Given a connected graph with V vertices, find a set of V-1 edges that connects the vertices Any DFS is a spanning tree Two coloring, bipartiteness check

Separability and Connectivity Bridge An edge that, if removed, would separate a connected graph into two disjoint subgraphs. Edge-connected graph – has no bridges In a DFS tree, edge v-w is a bridge iff there are no back edges that connect a descendant of w to an ancestor of w T T G S G S E R A E R A

Separability and Connectivity Articulation point (separation/cut) Removal results in at least two disjoint subgraphs K-connected - for each pair: At least k vertex disjoint paths Indicates the number of vertices that need to be removed to disconnect a graph Biconnected : 2-connected removal of a vertex does not disconnect K-edge-connected - for each pair: At least k edge disjoint paths Indicates the number of edges that need to be removed to disconnect a graph T G S E R A

BFS Search Instead of Stack Can be used to solve Use a Queue Connected components Spanning tree Shortest paths #define bfs search void bfs(Graph G, Edge e) { int v, w; QUEUEput(e); while (!QUEUEempty()) if (pre[(e = QUEUEget()).w] == -1) { pre[e.w] = cnt++; st[e.w] = e.v; for (v = 0; v < G->V; v++) if (G->adj[e.w][v] == 1) if (pre[v] == -1) QUEUEput(EDGE(e.w, v)); }

Directed Graph Digraph: Vertices + directed edges In-degree: number of directed edge coming in Out-degree: number of directed edge going out DAG – no directed cycles Strongly connected Every vertex is reachable from every other Not strongly connected : set of strong components Kernel K(D) of digraph D One vertex of K(D) corresponds to each strong component of D One edge in K(D) corresponds to each edge in D that connects vertices in different components K(D) is a DAG

Reachability and Transitive closure for (i = 0; i < G->V; i++) for (s = 0; s < G->V; s++) for (t = 0; t < G->V; t++) if (A[s][i] && A[i][t] == 1) G->tc[s][t] = 1; Transitive closure of a graph Same vertices + an edge from s to t in transitive closure if there is a directed path from s to t Warshall’s algorithm Complexity: V3 void GRAPHtc(Graph G) { int i, s, t; G->tc = MATRIXint(G->V, G->V, 0); for (s = 0; s < G->V; s++) for (t = 0; t < G->V; t++) G->tc[s][t] = G->adj[s][t]; for (s = 0; s < G->V; s++) G->tc[s][s] = 1; for (i = 0; i < G->V; i++) if (G->tc[s][i] == 1) if (G->tc[i][t] == 1) G->tc[s][t] = 1; }

Topological Sort Given a DAG Renumber vertices such that every directed edge points from a lower-numbered vertex to a higher-number one 6 6 2 2 1 1 7 5 8 4 3 3 relabel 4 7 5 8

Topological Sort Process each vertex before processing the vertices it points Reverse topological sort Scheduling applications Postorder numbering in DFS yields a reverse topological sort 1 2 3 8 7 6 4 5 rearrange

Topological Sort // Reverse (adj list) static int cnt0; static int pre[maxV]; void DAGts(Dag D, int ts[]) { int v; cnt0 = 0; for (v = 0; v < D->V; v++) { ts[v] = -1; pre[v] = -1; } if (pre[v] == -1) TSdfsR(D, v, ts); } void TSdfsR(Dag D, int v, int ts[]) { link t; pre[v] = 0; for (t = D->adj[v]; t != NULL; t = t->next) if (pre[t->v] == -1) TSdfsR(D, t->v, ts); ts[cnt0++] = v; // Adj. matrix void TSdfsR(Dag D, int v, int ts[]) { int w; pre[v] = 0; for (w = 0; w < D->V; w++) if (D->adj[w][v] != 0) if (pre[w] == -1) TSdfsR(D, w, ts); ts[cnt0++] = v; }

Minimum Spanning Tree Weighted graph To incorporate this information into the graph, a weight, usually a positive integer, is attached to each arc capacity, length, traversal time, or traversal cost.

Minimum Spanning tree (MST) A spanning tree whose weight (the sum of the weights in its edges) is no larger than the weight of any other spanning tree Representation weighted graph using an adjacency matrix is straightforward – use an integer matrix In the adjacency list representation, the elements of the list now have two components, the node and the weight of the arc

MST A graph and its MST

MST N1 N2 A Cut Cut Property X A partition of the vertices into two disjoint sets Crossing edge is one that connects a vertex in one set with a vertex in the other Cut Property Given some cut in a graph, every minimal crossing edge belongs to some MST of the graph, and every MST contains a minimal crossing edge N1 N2 X

Cut Property Proof: Suppose that on the contrary, there is no minimum spanning tree that contains X. Take any minimum spanning tree and add the arc X to it. N1 N2 X Y A cycle is formed after adding X.

Cycle Property Cycle property Given a graph G, consider the graph G’ defined by adding an edge e to G Adding e to an MST of G and deleting a maximal edge on the resulting cycle gives an MST of G’

Prim’s algorithm Prim’s algorithm time complexity: Step 1: x  V, Let A = {x}, B = V - {x} Step 2: Select (u, v)  E, u  A, v  B such that (u, v) has the smallest weight between A and B Step 3: (u, v) is in the tree. A = A  {v}, B = B - {v} Step 4: If B = , stop; otherwise, go to Step 2. time complexity: O(n2), n = |V|.

Prim’s Algorithm

Kruskal’s algorithm Step 1: Sort all edges Step 2: Add the next smallest weight edge to the forest if it will not cause a cycle. Step 3: Stop if we have n-1 edges. Otherwise, go to Step2.

Shortest Path The shortest path problem has several different forms: Given two nodes A and B, find the shortest path in the weighted graph from A to B. Given a node A, find the shortest path from A to every other node in the graph. (single-source shortest path problem) Find the shortest path between every pair of nodes in the graph. (all-pair shortest path problem)

Shortest Path Visit the nodes in order of their closeness; visit A first, then visit the closest node to A, then the next closest node to A, and so on. Dijkstra’s algorithm

Shortest path To select the next node to visit, we must choose the node in the fringe that has the shortest path to A. The shortest path from the next closest node must immediately go to a visited node. Visited nodes form a shortest path tree Fringe node set