Graphs - according to the mathematicians An undirected graph is 2-tuple: G=(V,E) a set of vertices a set of edges Vertices = {A, B, C, D, E} Edges = {

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
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.
Data Structures Using C++
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.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
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.
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.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
TCOM 501: Networking Theory & Fundamentals
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 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.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Data Structures Using C++ 2E
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Graphs – ADTs and Implementations ORD DFW SFO LAX
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Graphs Chapter 12.
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,
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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 1 Graph Categories Strong Components Example of Digraph
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.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
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:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
Minimum Spanning Tree Chapter 13.6.
C.Eng 213 Data Structures Graphs Fall Section 3.
CS202 - Fundamental Structures of Computer Science II
Connected Components Minimum Spanning Tree
Graphs Chapter 13.
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
Autumn 2016 Lecture 10 Minimum Spanning Trees
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPHS.
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:

Graphs - according to the mathematicians An undirected graph is 2-tuple: G=(V,E) a set of vertices a set of edges Vertices = {A, B, C, D, E} Edges = { {A,B}, {A,C}, {A,D}, {B,D} } Vertices = Edges = Can a graph...contain an edge from A to A?...contain two edge from A to B?

Graphs - more definitions An edge is a pair (v,w), where v, w V Path: is a walk in which all vertices are unique A walk is an alternating sequence of vertices and edges (starting and ending with vertices) in which each edge is incident to the vertices that precede and follow it in the sequence. e1e1 e2e2 e3e3 e4e4

Graphs - still more definitions e1e1 e2e2 e3e3 e4e4 Directed Graph (digraphs) Undirected Graph An undirected graph is connected if there is a path from every vertex to every other vertex

Labeled Graphs

Designing Graphs An undirected graph is 2-tuple: a set of vertices a set of edges Graph constructor» + Graph() «query» + boolean containsVertex( VertexKey v ) + boolean containsEdge(VertexKey v1, VertexKey v2 ) «update» + void insertVertex( VertexKey v ) + void insertEdge( VertexKey v1, VertexKey v2 ) + void removeVertex( VertexKey v ) + void removeEdge( VertexKey v1, VertexKey v2 ) Graph constructor» + Graph() «query» + boolean containsVertex( VertexKey v ) + boolean containsEdge(VertexKey v1, VertexKey v2 ) «update» + void insertVertex( VertexKey v ) + void insertEdge( VertexKey v1, VertexKey v2 ) + void removeVertex( VertexKey v ) + void removeEdge( VertexKey v1, VertexKey v2 )

Graph ADT Graph ADT Specifications Domain A Graph consists of two parts VERTICES -- a set of VertexKey EDGES -- a set of Invariant (ForAll  EDGES : v1  VERTICES and v2  VERTICES and v1 ≠ v2 ) and (ForAll  EDGES and  EDGES and ≠ : (v1 = v4)  (v2 ≠ v3) no loops no reversed edges

Graph ADT Methods Graph ADT Specifications(continued) Constructor Methods public Graph() post: VERTICES == {} and EDGES == {} Query Methods public boolean containsVertex(VertexKey v) post: result == ( v  VERTICES ) public boolean containsEdge(VertexKey v1, VertexKey v2) post: result == (ThereIs ed  EDGES : ed == or ed == )

Graph ADT Methods Graph ADT Specifications(continued) Update Methods public void insertVertex(VertexKey v) post: VERTICES ==  { v } public void insertEdge(VertexKey v1, VertexKey v2) pre: v1  VERTICES and v2  VERTICES and not containsEdge(v1,v2) post: EDGES =  { } public void removeVertex(VertexKey v) post: VERTICES ==  { v } and EDGES = - { e | e  and ( e = or e = for some vertex v s ) public void removeEdge(VertexKey v1, VertexKey v2) post: EDGES == - {, } What is different about a digraph ADT?

Digraph ADT Digraph ADT Specifications Domain A Digraph consists of two parts VERTICES -- a set of VertexKey ARCS -- a set of Invariant ( ForAll  ARCS : fromV  VERTICES and toV  VERTICES )

Digraph ADT Methods Digraph ADT Specifications(continued) Constructor Methods public Digraph() post: VERTICES == {} and ARCS == {} Query Methods public boolean containsVertex(VertexKey v) post: result == ( v  VERTICES ) public boolean containsArc(VertexKey fromV, VertexKey toV) post: result == (ThereIs arc  ARCS : arc == )

Digraph ADT Methods Digraph ADT Specifications(continued) Update Methods public void insertVertex(VertexKey v) post: VERTICES ==  { v } public void insertArc(VertexKey fromV, VertexKey toV) pre: v1  VERTICES and v2  VERTICES post: ARCS =  { } public void removeVertex(VertexKey v) pre: ForAll a ==  ARCS : ( fromV  v and toV  v ) post: VERTICES ==  { v } public void removeArc(VertexKey fromV, VertexKey toV) post: ARCS == - { }

Digraph Implementations Digraph ADT Specifications Domain A Digraph consists of two parts VERTICES -- a set of VertexKey ARCS -- a set of Invariant ( ForAll  ARCS : fromV  VERTICES and toV  VERTICES ) Implementation?

Using Integers as “Keys” Digraph constructor» + Digraph() «query» + boolean containsVertex( VertexKey v ) + boolean containsArc(VertexKey fromV, VertexKey toV) «update» + void insertVertex( VertexKey v ) + void insertArc( VertexKey fromV, VertexKey toV ) + void removeVertex( VertexKey v ) + void removeArc( VertexKey fromV, VertexKey toV ) Digraph constructor» + Digraph() «query» + boolean containsVertex( VertexKey v ) + boolean containsArc(VertexKey fromV, VertexKey toV) «update» + void insertVertex( VertexKey v ) + void insertArc( VertexKey fromV, VertexKey toV ) + void removeVertex( VertexKey v ) + void removeArc( VertexKey fromV, VertexKey toV ) Digraph constructor» + Digraph() «query» + boolean containsVertex( int v ) + boolean containsArc( int fromV, int toV ) «update» + void insertVertex( int v ) + void insertArc( int fromV, VertexKey toV ) + void removeVertex( int v ) + void removeArc( int fromV, int toV ) Digraph constructor» + Digraph() «query» + boolean containsVertex( int v ) + boolean containsArc( int fromV, int toV ) «update» + void insertVertex( int v ) + void insertArc( int fromV, VertexKey toV ) + void removeVertex( int v ) + void removeArc( int fromV, int toV )

Using Integers as “Keys” Question: How would you implement such numbering? Answer: Why does this eliminate the need to store vertices?

Graph Breadth-first search Depth-first search Topological ordering: an ordering of vertices in a directed acyclic graph A D E B C

Adjacency Lists Consider storing outgoing arcs “in” their vertices. Performance? [0] [1] [2] [3] [4] [5] [6]...insertArc...containsArc...removeVertex

Path Finder Methods Digraph constructor» + Digraph() «query» + boolean containsVertex( int v ) + boolean containsArc( int fromV, int toV ) + boolean pathExists( int fromV, int toV ) + int distance( int fromV, int toV ) «update» + void insertVertex( int v ) + void insertArc( int fromV, VertexKey toV ) + void removeVertex( int v ) + void removeArc( int fromV, int toV ) Digraph constructor» + Digraph() «query» + boolean containsVertex( int v ) + boolean containsArc( int fromV, int toV ) + boolean pathExists( int fromV, int toV ) + int distance( int fromV, int toV ) «update» + void insertVertex( int v ) + void insertArc( int fromV, VertexKey toV ) + void removeVertex( int v ) + void removeArc( int fromV, int toV )

pathExists public boolean pathExists(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; if (fromV == toV) // path of length zero return true; else { reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); for (int v=0; v<=vertexCount; v++) if ( containsArc(reachedVertex, v) ) if (v == toV) return true; else reachedQ.addLast( v ); } return false; } public boolean pathExists(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; if (fromV == toV) // path of length zero return true; else { reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); for (int v=0; v<=vertexCount; v++) if ( containsArc(reachedVertex, v) ) if (v == toV) return true; else reachedQ.addLast( v ); } return false; } How could performance be improved for an adjacency list representation? This is an example of a Depth-first algorithm

pathExists (for adjacency lists) public boolean pathExists(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; if (fromV == toV) // path of length zero return true; else { reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); for each (vertex, v, in the adjacency list of reachedVertex) if (v == toV) return true; else reachedQ.addLast( v ); } return false; } public boolean pathExists(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; if (fromV == toV) // path of length zero return true; else { reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); for each (vertex, v, in the adjacency list of reachedVertex) if (v == toV) return true; else reachedQ.addLast( v ); } return false; }

distance public int distance(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; if (fromV == toV) // path of length zero return 0; else { reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); for each (vertex, v, in the adjacency list of reachedVertex) if (v == toV) return step; else reachedQ.addLast( v ); } return -1; } public int distance(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; if (fromV == toV) // path of length zero return 0; else { reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); for each (vertex, v, in the adjacency list of reachedVertex) if (v == toV) return step; else reachedQ.addLast( v ); } return -1; } How does the method change for distance( int fromV, int toV )

distance - for a weighted digraph A common variant of distance is called shortest path. This algorithm begins with a labeled digraph in which arcs are labeled with numeric weights. Our first algorithm, known as Dijkstra’s Algorithm, assumes that all arc weights are positive. Modifications to prior algorithm: 1) must check all possible paths. 2) must compare each total path weight to shortest thus far. 3) must store total path weight along with each queued vertex.

Shortest Path public double shortestWeightedPath(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; int shortestPathSoFar = maxPossiblePath+1; if (fromV == toV) // path of length zero shortestPathSoFar = getArcWeight(fromV, toV); reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); reachedQ.addLast(getArcWeight(fromV, toV)); // if there is no Arc, weight: 0 for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); int pathLen = lastQ.removeFirst(); for each (vertex, v, in the adjacency list of reachedVertex) { lastQ.addLast( v ); lastQ.addLast( pathLen + getArcWeight(reachedVertex,v) ); if ( v == toV && && pathLen+getArcWeight(reachedVertex,v) < shortestPathSoFar ) shortestPathSoFar = pathLen+getArcWeight(reachedVertex,v); } return shortestPathSoFar; } public double shortestWeightedPath(int fromV, int toV) { java.util.LinkedList reachedQ, lastQ; int shortestPathSoFar = maxPossiblePath+1; if (fromV == toV) // path of length zero shortestPathSoFar = getArcWeight(fromV, toV); reachedQ = new java.util.LinkedList (); reachedQ.addLast( fromV ); reachedQ.addLast(getArcWeight(fromV, toV)); // if there is no Arc, weight: 0 for (int step=1; step<=vertexCount-1; step++) { lastQ = reachedQ; reachedQ = new java.util.LinkedList (); while (lastQ.size() != 0) { int reachedVertex = lastQ.removeFirst(); int pathLen = lastQ.removeFirst(); for each (vertex, v, in the adjacency list of reachedVertex) { lastQ.addLast( v ); lastQ.addLast( pathLen + getArcWeight(reachedVertex,v) ); if ( v == toV && && pathLen+getArcWeight(reachedVertex,v) < shortestPathSoFar ) shortestPathSoFar = pathLen+getArcWeight(reachedVertex,v); } return shortestPathSoFar; }

...with Negative Weights Question: What must change to allow for negative weights? Answer: Dijkstra’s algorithm does not work A B C

Multilist A multilist implementation: (1) stores vertices in a list. Example (2) stores arcs in adjacency lists, but using pointers back to from vertices.

Adjacency Matrices Yet another representation - one bit per potential arc. Performance? boolean[][] digraph = boolean[maxV][maxV]; [0] [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] [6]

Trees & Forests How would you define a tree (from an undirected graph)? A forest is an undirected graph consisting of zero or more unconnected components such that each component is a tree.

Spanning Tree A spanning tree for a particular graph is a tree that (1)includes all of the graph’s vertices and (2) includes a subset of the graph’s edges. Suggest example spanning trees.

Minimum Weight Spanning Tree A minimum weight spanning tree for a particular weighted graph is a spanning tree with total of all edge weights less than or equal to that of all other spanning trees for the graph. Applications?

Kruskal’s Algorithm Construct a sequence of forests (F 0, F 1,... F n ) using only vertices and edges from an original digraph D, such that (a) F 0 is the empty forest. (b) F j+1 is formed by adding an edge and incident vertices to F j. This edge must have minimum weight among D’s edges that are not in F j.

Kruskal’s Algorithm Example F 0 : the empty forest F 1 ?

Prim’s Algorithm Construct a sequence of trees (T 1, T 2,... T n ) using only vertices and edges from an original digraph D, such that (a) T 1 consists of any single vertex of D. (b) T j+1 is formed by adding an edge and vertex to T j. This edge must be have minimum weight among D’s edges that are not in T j and are incident to one of T j ’s vertices.

Prim’s Algorithm Example T 1 ?