CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
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.
CS202 - Fundamental Structures of Computer Science II
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Graph.
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,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) 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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE Discrete.
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.
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
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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,
1 CS104 : Discrete Structures Chapter V Graph Theory.
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.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graphs 9.1 Graphs and Graph Models أ. زينب آل كاظم 1.
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++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
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 Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Chapter 9: Graphs.
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.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Graphs Rosen, Chapter 8. NOT ONE OF THESE! One of these!
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.
Nattee Niparnan. 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,
Graphs Spring 2016CS202 - Fundamental Structures of Computer Science II1 Initially prepared by Dr. Ilyas Cicekli; improved by various Bilkent CS202 instructors.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
CS212: Data Structures and Algorithms
Graphs Representation, BFS, DFS
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
Graphs.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CS100: Discrete structures
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.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
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:

CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1

Colorado State University Outline Introduction Terminology Shortest Paths Implementing Graphs Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 2

CS200 Algorithms and Data StructuresColorado State University Graphs What can this represent? A computer network Abstraction of a map Social network A collection of nodes and edges

CS200 Algorithms and Data StructuresColorado State University Directed Graphs Sometimes we want to represent directionality: Unidirectional network connections One way streets The web A collection of nodes and directed edges

CS200 Algorithms and Data StructuresColorado State University 5

CS200 Algorithms and Data StructuresColorado State University Example “Follow the money” example (the international science and engineering visualization challenge, 2009, Science magazine and National Science Foundation) – Reference The Scaling Laws of Human Travel, D. Brockmann, L. Hufnagel and T. Geisel, Nature 439, 462 (2006) – 006nature.pdf 6

CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 7

CS200 Algorithms and Data StructuresColorado State University Graph Terminology Vertices/ Nodes Edges Two vertices are adjacent if they are connected by an edge. An edge is incident on two vertices Degree of a vertex: number of edges incident on it G=(V, E) v u e Vertices Edges Graph terminology: 14.1 in W&M, 9.1 in Rosen

CS200 Algorithms and Data StructuresColorado State University subgraph Graph Terminology A subgraph of a graph G = (V,E) is a graph (V’,E’) such that V’ is a subset of V and, E’ is a subset of E

CS200 Algorithms and Data StructuresColorado State University Paths Path: a sequence of edges (e 1, e 2, e 3 ) is a path of length 3 from v 1 to v 4 In a simple graph a path can be represented as a sequence of vertices v1v1 v4v4 v2v2 v3v3 e1e1 e2e2 e3e3

CS200 Algorithms and Data StructuresColorado State University Graph Terminology Self loop (loop): an edge that connects a vertex to itself Simple graph: no self loops and no two edges connect the same vertices Multigraph: may have multiple edges connecting the same vertices Pseudograph: multigraph with self-loops

CS200 Algorithms and Data StructuresColorado State University Directed Graphs Indegree: number of incoming edges Outdegree: number of outgoing edges w v

CS200 Algorithms and Data StructuresColorado State University Connected Components An undirected graph is called connected if there is a path between every pair of vertices of the graph. A connected component of a graph G is a connected subgraph of G that is not a proper subgraph of another connected subgraph of G. a b c d e f g G={{a,b,c,d,e,f,g},E} G 1 ={{a,b,c},E 1 } G 2 ={{d,e,f,g}, E 2 }

CS200 Algorithms and Data StructuresColorado State University The degree of a vertex The degree of a vertex in an undirected graph –the number of edges incident with it –except that a loop at a vertex contributes twice to the degree of that vertex. 14

CS200 Algorithms and Data StructuresColorado State University Example 15 a f e g d c b deg(a) = 2 deg(b) = deg(f) = 4 deg(d) = 1 deg(e) = 3 deg(g) = 0

CS200 Algorithms and Data StructuresColorado State University Complete Graphs Simple graph that contains exactly one edge between each pair of distinct vertices. 16 Complete Graph

CS200 Algorithms and Data StructuresColorado State University Cycles The cycle C n, n ≥ 3, consists of n vertices v 1, v 2, …, v n and edges {v 1, v 2 }, {v 2, v 3 },…., and {v n-1, v n }. 17

CS200 Algorithms and Data StructuresColorado State University Wheels We obtain the wheel W n when we add an additional vertex to the cycle C n, for n ≤ 3, and connect this new vertex to each of the n vertices in C n, by new edges 18

CS200 Algorithms and Data StructuresColorado State University n -Cubes ( n -dimensional hypercube) Hypercube

CS200 Algorithms and Data StructuresColorado State University Bipartite Graphs A simple graph G is called bipartite, if its vertex set V can be partitioned into two disjoint sets V 1 and V 2 such that every edge in the graph connects a vertex in V 1 and a vertex in V 2. 20

CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 21

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm) Graph G(V,E) with non-negative weights (“distances”) Compute shortest distances from vertex s to every other vertex in the graph

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm) Algorithm –Maintain array d (minimum distance estimates) Init: d[s]=0, d[v]=  v  V-s –Priority queue of vertices not yet visited –select minimum distance vertex, visit v, update neighbors

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm) a a b b e e c c d d

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): Initialize a b c e d a b c d e 0 ∞ ∞ ∞ ∞

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step a b c e d a b c d e 0 10/a 5/a a b c d e 0 10/a 5/a a b c d e 0 ∞ ∞ ∞ ∞

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step a b c e d a b c d e 0 10/a 5/a /c 5/a 7/c 14/c

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step a b c e d a b c d e 0 10/a 5/a /c 5/a 7/c 14/c 0 8/c 5/a 7/c 11/d

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step a b c e d a b c d e 0 10/a 5/a /c 5/a 7/c 14/c 0 8/c 5/a 7/c 11/d 0 8/c 5/a 7/c 9/d

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): Done a b c e d a b c d e 0 8/c 5/a 7/c 9/b Minimum distance from a to b : 8 Minimum distance from a to c: 5 Minimum distance from a to d: 7 Minimum distance from a to e: 9

CS200 Algorithms and Data StructuresColorado State University Example 31

CS200 Algorithms and Data StructuresColorado State University Dijkstra’s Algorithm Dijkstra(G: graph with vertices v 0 …v n-1 and weights w[u][v]) // computes shortest distance of vertex 0 to every other vertex create a set vertexSet that contains only vertex 0 d[0] = 0 for (v = 1 through n-1) d[v] = infinity for (step = 2 through n) find the smallest d[v] such that v is not in vertexSet add v to vertexSet for (all vertices u not in vertexSet) if (d[u] > d[v] + w[v][u]) d[u] = d[v] + w[v][u]

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms Using a Priority Queue (Dijkstra’s Algorithm): step a b c e d [5,c],[10,b]

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step a b c e d [7,d],[8,b],[14,e]

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step a b c e d [8,b],[11,e]

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): step a b d c e [9,c]

CS200 Algorithms and Data StructuresColorado State University Shortest Path Algorithms (Dijkstra’s Algorithm): Done a b d c e

CS200 Algorithms and Data StructuresColorado State University Dijkstra’s Algorithm How to obtain the shortest paths? –At each vertex maintain a pointer that tells you the vertex from which you arrived.

CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 39

CS200 Algorithms and Data StructuresColorado State University Graph ADT Create Empty? Number of vertices? Number of edges? Edge exists between two vertices? Add a vertex Add an edge Delete a vertex (and any edges adjacent to it) Delete an edge Retrieve a vertex

CS200 Algorithms and Data StructuresColorado State University Classes for a Weighted Undirected Graph Vertex:??? Edge: ??? Graph: –organized collection of vertices and edges 1.Adjacency Matrix Implementation 2.Adjacency List Implementation

CS200 Algorithms and Data StructuresColorado State University Adjacency Matrix Implementation A BC E D mapping of vertex labels to array indices LabelIndex A0 B1 C2 D3 E Adjacency Matrix: indicates whether an edge exists between two vertices Vertices Edges square matrix of edges Possible Java implementation: int [][] A;

CS200 Algorithms and Data StructuresColorado State University Adjacency Matrix Implementation Directed graph: A[i][j] is 1 if there is an edge from vertex i to vertex j Undirected graph: A[i][j] is 1 if there is an edge between vertex i and vertex j. What’s the relationship between A[i][j] and A[j][i]? Weighted graph: A[i][j] gives the weight of the edge

CS200 Algorithms and Data StructuresColorado State University Example Adjacency matrix? d e f g defg d0111 e1001 f1000 g1100

CS200 Algorithms and Data StructuresColorado State University Adjacency List Implementation A BC E D mapping of vertex labels to list of edges IndexLabel 0A 1B 2C 3D 4E B D E A B C Each vertex has a list of outgoing edges

CS200 Algorithms and Data StructuresColorado State University Adjacency List Implementation For undirected graphs each edge appears twice. Why?

CS200 Algorithms and Data StructuresColorado State University Which Implementation? Which implementation best supports common Graph Operations: –Is there an edge between vertex i and vertex j? –Find all vertices adjacent to vertex j Which best uses space?

CS200 Algorithms and Data StructuresColorado State University Implementation: Edge Class Class Edge { private Integer v,w; // vertices private int weight; public Edge(Integer first, Integer second, int edgeWeight){ v = first; w = second; weight = edgeWeight; } public int getWeight() { return weight; } public Integer getV() { return v; } public Integer getW() { return w; }

CS200 Algorithms and Data StructuresColorado State University Implementation: Graph Class class Graph { private int numVertices; private int numEdges; private Vector > adjList;TreeMap public Graph(int n) { numVertices = n; numEdges = 0; adjList = new Vector >(); for (int i=0; i<numVertices; i++) { adjList.add(new TreeMap ()); } }

CS200 Algorithms and Data StructuresColorado State University Implementation: Graph Class public void addEdge(Integer v, Integer, w, int weight){ // precondition: the vertices v and w must exist //postcondition: the edge (v,w) is part of the graph adjList.get(v).put(w, weight); adjList.get(w).put(v, weight); numEdges++; }

CS200 Algorithms and Data StructuresColorado State University Outline Introduction Terminology Implementing Graphs Shortest Paths Properties Graph Traversals Topological Sorting Spanning Trees Minimum Spanning Trees Circuits 51

CS200 Algorithms and Data StructuresColorado State University Graph Traversal ABCD EFGH IJKL MNOP

CS200 Algorithms and Data StructuresColorado State University Depth First Search ABCD EFGH IJKL MNOP

CS200 Algorithms and Data StructuresColorado State University Graph Traversal Depth First Search (DFS) dfs(in v:Vertex) mark v as visited for (each unvisited vertex u adjacent to v) dfs(u) Need to track visited nodes Order of visiting nodes is not completely specified Is there a difference between directed undirected graphs? Which graph implementation? Depth first search

CS200 Algorithms and Data StructuresColorado State University Iterative DFS dfs(in v:Vertex) s – stack for keeping track of active vertices s.push(v) mark v as visited while(!s.isEmpty()) { if (no unvisited vertices adjacent to the vertex on top of the stack) { s.pop() \\backtrack\\backtrack else { select unvisited vertex u adjacent to vertex on top of the stack s.push(u) mark u as visited } }

CS200 Algorithms and Data StructuresColorado State University Graph Traversal – Breadth First Search (BFS) ABCD EFGH IJKL MNOP Breath First Search

CS200 Algorithms and Data StructuresColorado State University BFS Similar to level order tree traversal DFS is a last visited first explored strategy BFS is a first visited first explored strategy

CS200 Algorithms and Data StructuresColorado State University BFS bfs(in v:Vertex) q – queue of nodes to be processed q.enque(v) mark v as visited while(!q.isEmpty()) { w = q.dequeue() for (each unvisited vertex u adjacent to w) { mark u as visited q.enqueue(u) } }

CS200 Algorithms and Data StructuresColorado State University Trace this example ABCD EFGH IJKL MNOP bfs(in v:Vertex) q – queue of nodes to be processed q.enque(v) mark v as visited while(!q.isEmpty()) { w = q.dequeue() for (each unvisited vertex u adjacen t to w) { mark u as visited q.enqueue(u) } }

CS200 Algorithms and Data StructuresColorado State University Graph Traversal Properties of BFS and DFS: –Visit all vertices that are reachable from a given vertex –Therefore DFS(v) and BFS(v) visit a connected component Computation time for DFS, BFS for a connected graph: O(|V| + |E|)

CS200 Algorithms and Data StructuresColorado State University Reachability –v is reachable from u if there is a (directed) path from u to v –solve using BFS or DFS Transitive Closure (G*) –G* has edge from u to v if v is reachable from u.