Graph Algorithms DS.GR.1 Chapter 9 Overview Representation

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

DS.GR.14 Graph Matching Input: 2 digraphs G1 = (V1,E1), G2 = (V2,E2) Questions to ask: 1.Are G1 and G2 isomorphic? 2.Is G1 isomorphic to a subgraph of.
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.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Chapter 9: Graphs Basic Concepts
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Intro to Graphs CSIT 402 Data Structures II. CSIT 402 Graph Introduction2 Graphs Graphs are composed of ›Nodes (vertices) Can be labeled ›Edges (arcs)
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
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.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Lecture 19 CS2110 – Spring 2013.
Basic Concepts Graphs For more notes and topics visit:
Introduction to Graphs
CSE373: Data Structures & Algorithms Lecture 23: Applications
CS202 - Fundamental Structures of Computer Science II
Graph Algorithms Using Depth First Search
Graph Operations And Representation
CHP-7 GRAPH.
Graph & BFS.
Graphs Lecture 18 CS2110 – Fall 2009.
Graphs Chapter 13.
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Chapter 9: Graphs Basic Concepts
Graphs.
DS.GR.14 Graph Matching Input: 2 digraphs G1 = (V1,E1), G2 = (V2,E2)
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graph Operations And Representation
Chapter 11 Graphs.
2017, Fall Pusan National University Ki-Joune Li
CC 215 Data Structures Graph Terminology
CSE 373 Data Structures Lecture 17
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
Text Book: Introduction to algorithms By C L R S
Graphs G = (V, E) V are the vertices; E are the edges.
Graph Terminology CSE 373 Data Structures.
Data Structures and Algorithm Analysis Graph Algorithms
GRAPHS Lecture 17 CS2110 Spring 2018.
Graph Operations And Representation
Chapter 9: Graphs Basic Concepts
CSE 373 Data Structures Lecture 13
Chapter 9 Graph algorithms
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:

Graph Algorithms DS.GR.1 Chapter 9 Overview Representation Definitions Representation Topological Sort Graph Matching (for Program 3) Shortest Path Algorithms Network Flow Problems Minimum Spanning Trees Depth-First and Breadth-First Search NP-Completeness

DS.GR.2 Graphs and Digraphs A graph is a pair G = (V,E) where V is a set of vertices (or nodes) E is a set of edges (or arcs) Example: V = {a, b, c, d} E = { (a,b), (b,a),(a,c) (c,a),(b,c),(c,b) (c,d), (d,c),(c,c)} or E = { {a,b}, {a,c} {b,c}, {c,d} {c,c} } edge a b c d vertex An (undirected) graph represents a symmetric relation.

DS.GR.3 A digraph G = (V,E) is a graph in which the edges are directed from the first node to the second. Example: V = {a, b, c, d} E = { (a,b), (a,c), (b,c),(c,a),(c,c),(d,c) } a b d c vertex or node directed edge (or arc) This graph represents a binary relation that is not symmetric.

DS.GR.4 More Examples Undirected Graph Directed Graph I405 Lynnwood Woodinville I5 I520 I405 Seattle I5 Bellevue Tacoma M126 142 143 321 322 373 326 EE562 415

DS.GR.5 Some Additional Digraph Terminology arc A from-node of A to-node of A a b outdegree(a) = 3 indegree(a) = 0 outdegree(c) = 0 indegree(c) = 2 d c Node a is a source; Node c is a sink. For graphs, we just have the degree of a node.

DS.GR.6 Paths through Graphs A path in a graph is a sequence of vertices w1, w2, … , wN such that {wi,w[i+1]} is in E for 1  i < N. The length of the path is N–1, the number of edges on the path. b d a f c e A path from a node to itself with no repeated edges is a cycle. What are the cycles of this graph? What are all the paths from a to f?

DS.GR.7 Paths through Digraphs A path in a digraph is a sequence of vertices w1, w2, … , wN such that (wi,w[i+1]) is in E for 1  I < N. The only difference is moving along directed edges in the proper direction. b d a f c e What are the cycles of this digraph? What are all the paths from a to f? This is an acyclic digraph.

DS.GR.8 Graph Representations N x N Adjacency Matrix for N node graph Digraphs: 1 if there is an arc from node i to node j A[i,j] = 0 otherwise 1 2 3 4 1 2 1 2 3 4 0 1 0 0 0 1 1 0 0 0 0 1 3 4

DS.GR.9 Graphs: 1 if there is an edge connecting node i and node j A[i,j] = 0 otherwise 1 2 3 4 1 2 1 2 3 4 ?? 3 4 Note: since A[i,j] = 1 iff A[j,i] = 1, we only need to store half the matrix.

DS.GR.10 Adjacency Matrices Are usually bit matrices, unless the graph has weights on the edges, in which case the 1-bits are replaced by the weights. Are used in certain algorithms that make use of simple matrix operations, such as AND and OR. Are inefficient for some algorithms, because a. They waste space when the graph is sparse b. You have to search a whole row to find those nodes adjacent to a given one.

DS.GR.11 2. Linked Representation: Adjacency Lists N element array of lists V[i] points to a list of nodes that are adjacent to node i. For digraphs, this is usually the list of nodes reachable by following one arc out of node i. But, we can have another set of lists for nodes whose arcs go into node i. 1 2 3 4 4 7 What does this structure tell us for a graph? For a digraph? 2

DS.GR.12 Topological Sort Given an acyclic digraph G, where (<<) (Ni,Nj)  E means that Ni precedes Nj Find an ordering of the nodes N1, N2, N3, . . . , Nn so that N1 << N2 << N3 << . . . << Nn. 415 142 143 373 562 576 linear algebra 457 Find an ordering in which all these courses can be taken, satisfying their prerequisites.

DS.GR.13 Complexity of Topological Sort Assuming the adjacency list representation, The indegree of each vertex is computed in an initialization step. |V| Each node will go into the queue and come out exactly once. |V| Each edge will be examined once (in the for loop when its from-node is processed. |E| So the complexity is O(|V| + |E|).

DS.GR.14 Graph Matching Input: 2 digraphs G1 = (V1,E1), G2 = (V2,E2) Questions to ask: Are G1 and G2 isomorphic? Is G1 isomorphic to a subgraph of G2? How similar is G1 to G2? How similar is G1 to the most similar subgraph of G2?

DS.GR.15 Isomorphism for Digraphs G1 is isomorphic to G2 if there is a 1-1, onto mapping h: V1  V2 such that ( vi,vj )  E1 iff ( h(vi), h(vj) )  E2 G1 G2 1 2 a b 3 c 4 5 e d Find an isomorphism h: {1,2,3,4,5}  {a,b,c,d,e}. Check that the condition holds for every edge.

DS.GR.16 Subgraph Isomorphism for Digraphs G1 is isomorphic to a subgraph of G2 if there is a 1-1 mapping h: V1  V2 such that ( vi,vj )  E1 ( h(vi), h(vj) )  E2 G1 G2 1 2 a b 3 c d Isomorphism and subgraph isomorphism are defined similarly for undirected graphs. In this case, when (vi,vj)  E1, either (vi,vj) or (vj,vi) can be listed in E2, since they are equivalent and both mean {vi,vj}.

DS.GR.17 Similar Digraphs Sometimes two graphs are close to isomorphic, but have a few “errors." Let h(1)=b, h(2)=e, h(3)=c, h(4)=a, h(5) = d. G1 G2 1 2 a b 3 c 4 5 e d (1,2) (b,e) (2,1) (e,b) X (c,b) (4,5) (a,d) (2,5) (e,d) (3,2) X (3,4) (c,a) The mapping h has 2 errors. (c,b)  G2, but (3,1)  G1 (3,2)  G1, but (c,e)  G2

Intuitively, the error of mapping h tells us DS.GR.18 Error of a Mapping Intuitively, the error of mapping h tells us how many edges of G1 have no corresponding edge in G2 and how many edges of G2 have no corresponding edge in G1. Let G1=(V1,E1) and G2=(V2,E2), and let h:V1 V2 be a 1-1, onto mapping. forward error backward total error relational distance EF(h) = |{(vi,vj)E1 | (h(vi),h(vj))E2}| edge in E1 corresponding edge not in E2 -1 -1 EB(h) = |{(vi,vj)E2 | (h (vi),h (vj))E1}| edge in E2 corresponding edge not in E1 Error(h) = EF(h) + EB(h) GD(G1,G2) = min Error(h) for all 1-1, onto h:V1 V2

DS.GR.19 Variations of Relational Distance normalized relational distance: Divide by the sum of the number of edges in E1 and those in E2. undirected graphs: Just modify the definitions of EF and EB to accommodate. one way mappings: h is 1-1, but need not be onto Only the forward error EF is used. labeled graphs: When nodes and edges can have labels, each node should be mapped to a node with the same label, and each edge should be mapped to an edge with the same label.

DS.GR.20 Graph Matching Algorithms graph isomorphism subgraph isomorphism relational distance attributed relational distance (uses labels) * Subgraph Isomorphism Given model graph M = (VM,EM) data graph D = (VD,ED) Find 1-1 mapping h:VM  VD satisfying (vi,vj)  EM  ((h(vi),h(vj))  ED.

Method: Backtracking Tree Search DS.GR.21 Method: Backtracking Tree Search M D 1 2 a b c 3 e d root 1,a 1,b 1,c 1,d 1,e . . . . . . . . . 2,b 2,c . . . 2,a 2,c X X 3,c 3,d 3,e 3,a 3,d X X X X YES!

DS.GR.22 Treesearch for Subgraph Isomorphism in Digraphs procedure Treesearch(VM, VD, EM, ED, h) { v = first(VM); for each w  VD h’ = h  {(v,w)}; OK = true; for each edge (vi,vj) in EM satisfying that either 1. vi = v and vj  domain(h’) or 2. vj = v and vi  domain(h’) if ( (h’(vi),h’(vj))  ED ) {OK = false; break;}; if OK { VM’ = VM – v; VD’ = VD – w’ if isempty(VM’) output(h’); else Treesearch(VM’,VD’,EM,ED,h’) } } }

Branch-and-Bound Tree Search Keep track of the least-error mapping. DS.GR.23 Branch-and-Bound Tree Search Keep track of the least-error mapping. M D 1 2 a b c 3 d map_err = 0 bound_err = 99999 root map_err = 0 map_err = 0 1,a 1,b . . . map_err = 1 map_err = 0 2,b 2,c 2,d 2,a 2,c 2,d X X X X 3,c 3,a 3,c X map_err = 1 bound_err = 1 mapping = {(1,a)(2,b)(3,c)} map_err = 0; bound_err = 1 mapping = {(1,b)(2,d)(3,c)}