CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

Similar documents
(5.2) 151 Math Exercises. Graph Terminology and Special Types of Graphs. Malek Zein AL-Abidin

Graphs. Pseudograph: multiple edges and loops allowed

Graphs. Introduction To Graphs: Exercises. Definitions:

DEFINITION OF GRAPH GRAPH THEORY GRAPHS ACCORDING TO THEIR VERTICES AND EDGES EXAMPLE GRAPHS ACCORDING TO THEIR VERTICES AND EDGES

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CPCS Discrete Structures 1

CMSC 380. Graph Terminology and Representation

Discrete Structures CISC 2315 FALL Graphs & Trees

Elements of Graph Theory

Section 8.2 Graph Terminology. Undirected Graphs. Definition: Two vertices u, v in V are adjacent or neighbors if there is an edge e between u and v.

CS 311 Discrete Math for Computer Science Dr. William C. Bulko. Graphs

Varying Applications (examples)

Math.3336: Discrete Mathematics. Chapter 10 Graph Theory

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Introduction III. Graphs. Motivations I. Introduction IV

Graphs. The ultimate data structure. graphs 1

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Discrete Mathematics (2009 Spring) Graphs (Chapter 9, 5 hours)

CHAPTER 2. Graphs. 1. Introduction to Graphs and Graph Isomorphism

GRAPHS, GRAPH MODELS, GRAPH TERMINOLOGY, AND SPECIAL TYPES OF GRAPHS

Introduction to Graphs

Characterizing Graphs (3) Characterizing Graphs (1) Characterizing Graphs (2) Characterizing Graphs (4)

Copyright 2000, Kevin Wayne 1

Basic Graph Definitions

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

GRAPH THEORY - FUNDAMENTALS

Chapter 1 Graph Theory

Graphs & Digraphs Tuesday, November 06, 2007

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

Discrete mathematics II. - Graphs

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV

Graphs. The ultimate data structure. graphs 1

Chapter 9 Graph Algorithms

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Graphs. Edges may be directed (from u to v) or undirected. Undirected edge eqvt to pair of directed edges

Approximation slides 1. An optimal polynomial algorithm for the Vertex Cover and matching in Bipartite graphs

Algorithm Design and Analysis

March 20/2003 Jayakanth Srinivasan,

Lecture 5: Graphs & their Representation

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Chapter 9 Graph Algorithms

BIL694-Lecture 1: Introduction to Graphs

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Basics of Graph Theory

Ma/CS 6b Class 5: Graph Connectivity

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

1 Digraphs. Definition 1

3.1 Basic Definitions and Applications

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Graph Algorithms. Definition

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

Foundations of Discrete Mathematics

Introduction to Graph Theory

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

Algorithms: Graphs. Amotz Bar-Noy. Spring 2012 CUNY. Amotz Bar-Noy (CUNY) Graphs Spring / 95

W4231: Analysis of Algorithms

Minimum Spanning Trees My T. UF

CS 561, Lecture 9. Jared Saia University of New Mexico

Outline. Graphs. Divide and Conquer.

3.1 Basic Definitions and Applications

Single Source Shortest Paths

Lecture 22 Tuesday, April 10

Graph and Digraph Glossary

Graph Theory CS/Math231 Discrete Mathematics Spring2015

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Week 12: Minimum Spanning trees and Shortest Paths

11/26/17. directed graphs. CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10. undirected graphs

Minimum Spanning Trees Ch 23 Traversing graphs

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

Graphs II: Trailblazing

Introduction to Graphs

MATH 363 Final Wednesday, April 28. Final exam. You may use lemmas and theorems that were proven in class and on assignments unless stated otherwise.

Algorithms. Graphs. Algorithms

A good picture is worth a thousand words

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

GRAPHS Lecture 19 CS2110 Spring 2013

MTL 776: Graph Algorithms. B S Panda MZ 194

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Chapter 2 Graphs. 2.1 Definition of Graphs

GRAPHS Lecture 17 CS2110 Spring 2014

Ordinary Differential Equation (ODE)

Review: Graph Theory and Representation

Combinatorics Summary Sheet for Exam 1 Material 2019

Chapter 11: Graphs and Trees. March 23, 2008

1. a graph G = (V (G), E(G)) consists of a set V (G) of vertices, and a set E(G) of edges (edges are pairs of elements of V (G))

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Graphs (MTAT , 6 EAP) Lectures: Mon 14-16, hall 404 Exercises: Wed 14-16, hall 402

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

15 211: RECITATION7, SECTION M. Hui Han Chin,

Reference Sheet for CO142.2 Discrete Mathematics II

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Graph Theory S 1 I 2 I 1 S 2 I 1 I 2

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Graph. Vertex. edge. Directed Graph. Undirected Graph

Transcription:

CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1

Graphs A collection of nodes and edges What can this represent? n A computer network n Abstraction of a map n Social network CS200 - Graphs 2

Directed Graphs A collection of nodes and directed edges Sometimes we want to represent directionality: n Unidirectional network connections n One way streets n The web CS200 - Graphs 3

Graphs/Networks Around Us http://lin-ear-th-inking.blogspot.com/2010/12/ visualizing-geodetic-information-with.html http://noduslabs.com/wp-content/uploads/2011/12/ figure-5-meaning-circulation.png CS200 - Graphs 4

Graph Terminology G=(V, E) Vertices Edges Edges u Two vertices (or nodes) are adjacent if they are connected by an edge. An edge is incident on two vertices, an edge e can be represented by two vertices (u,v) Degree of a vertex or node: number of edges incident on it Vertices/ Nodes e v Graph terminology: 14.1 in Prichard, 10.1 in Rosen CS200 - Graphs 5

Undirected Graphs n Undirected graph. G = (V, E) q V = set of nodes. q E = set of edges between pairs of nodes. q Captures pairwise relationship between objects. q Graph size parameters: n = V, m = E. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11 6

Directed Graphs n Directed graph. G = (V, E) q Edge (u, v) goes from node u to node v. n Example. Web graph - hyperlink points from one web page to another. q Modern web search engines exploit hyperlink structure to rank web pages by importance (pagerank). 7

Graph definitions Graph G = (V, E), V: set of nodes or vertices, E: set of edges (pairs of nodes). In an undirected graph, edges are unordered pairs (sets) of nodes. In a directed graph edges are ordered pairs (2-tuples) of nodes. Path: sequence of nodes (v 0..v n ) s.t. i: (v i,v i+1 ) is an edge. Path length: number of edges in the path, or sum of weights. Simple path: all nodes distinct. Cycle: path with first and last node equal. Acyclic graph: graph without cycles. DAG: directed acyclic graph. Two nodes are adjacent if there is an edge between them. In a complete graph all nodes in the graph are adjacent.

More definitions An undirected graph is connected if for all nodes v i and v j there is a path from v i to v j. G (V, E ) is a sub-graph of G(V,E) if V V and E E The sub-graph of G induced by V has all the edges (u,v) E such that u V and v V. In a weighted graph the edges have a weight (cost, length,..) associated with them.

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 induced subgraph The sub-graph of G induced by V has all the edges (u,v) E such that u V and v V. CS200 - Graphs 10

Question n A Tree is a subtype (special type) of Graph. A. True B. False CS200 - Graphs 11

Paths v 1 e 1 v 2 e 2 v 3 e 3 v 4 n n n n Path: a sequence of edges, e.g. ((v 1,v 2 ), (v 2,v 3 ), (v 3,v 4 )) s.t. the first node in the next edge is the second node in the previous edge. A simple path passes through a vertex only once. (e 1, e 2, e 3 ) is a simple path of length 3 from v 1 to v 4 A path can be represented by a sequence of vertices, here (v 1,v 2,v 3,v 4 ) CS200 - Graphs 12

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 (E is a set, so no multiples). We are mostly thinking about these. Multigraph: may have multiple edges connecting the same vertices (not a graph: E is a set in graph ) Pseudograph: multigraph with self-loops CS200 - Graphs 13

Complete Graphs n Simple graph that contains exactly one edge between each pair of distinct vertices. Complete Graph CS200 - Graphs 14

Question Which describes this graph? A. Simple B. Pseudograph C. Cycle D. Complete b f c e CS200 - Graphs 15

Question Which describes this graph? A. Simple B. Pseudograph C. Cycle D. Complete b c a f e CS200 - Graphs 16

Cycles The cycle C n, n 3, consists of n vertices v 1, v 2,, v n and n edges {v 1, v 2 }, {v 2, v 3 },, {v n-1, v n }, {v n, v 1 }. CS200 - Graphs 17

Wheels n We obtain the wheel W n when we add an additional vertex to the cycle C n, and connect this new vertex to each of the n vertices in C n, by new edges CS200 - Graphs 18

n-cube (n-dimensional hypercube) Hypercube 110 111 100 010 101 011 000 001 CS200 - Graphs 19

The degree of a vertex n The degree of a vertex in an undirected graph q the number of edges incident with it q except that a loop at a vertex contributes twice to the degree of that vertex. CS200 - Graphs 20

Example b c d d is pendant a f e g g is isolated deg(a) = 2 deg(b) = deg(f) = 4 deg(d) = 1 deg(e) = 3 deg(g) = 0 CS200 - Graphs 21

Question What is the degree of c? A. 4 B. 5 C. 6 b c d a f e g CS200 - Graphs 22

Directed Graphs w Indegree: number of incoming edges Outdegree: number of outgoing edges v CS200 - Graphs 23

Some Graph Theorems n Handshaking: Let G=(V,E) be an undirected graph with m edges. Then 2m = v V deg(v) n An undirected graph has an even number of vertices of odd degree. n Let G=(V,E) be a directed graph. Then deg (v) = deg + (v) = v V v V E CS200 - Graphs 24

Bipartite Graphs n n n A simple graph on which the vertex set V can be partitioned into two disjoint sets V1 and V2 such that every edge connects a vertex in V1 to one in V2. Bipartite? f g a e b d c Theorem: A simple graph is bipartite iff it is possible to assign one of two different colors to each vertex of the graph so that no two adjacent vertices are assigned the same color. f a e b d c CS200 - Graphs 25

Bipartite Graphs n Assign colors f g a e b d c f a e b d c Theorem: A graph G is bipartite iff it contains no odd cycle CS200 - Graphs 26

Question n Is this graph bipartite? A. Yes B. No a e c d f b CS200 - Graphs 27

Connected Components n n 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. G={{a,b,c,d,e,f,g},E} a c d b G 1 ={{a,b,c},e 1 } G 2 ={{d,e,f,g}, E 2 } e f g CS200 - Graphs 28

Question n How many connected components does it have? A. 0 B. 1 C. 2 a e c d f b CS200 - Graphs 29

Connectedness in Directed Graphs n A directed graph is strongly connected if there is a path from a to b and from b to a for all vertices a and b in the graph. n A directed graph is weakly connected if there is a path between every two vertices in the underlying undirected graph. CS200 - Graphs 30

A/B strongly/weakly connected? a d a d e e b c b c Graph A Graph B CS200 - Graphs 31

Graph Data Structures - Adjacency Matrix n Vertices q row and column indices mapped to labels q one vertex mapped to one index n Edges q entries in a square matrix n n size = (number of vertices)^2 edge: two (vertex) indices q values: n n boolean to indicate presence/absence of edge in (un)directed graph int to indicate value of weighted edge (0: no edge) n useful for dense graphs CS200 - Graphs 32

Adjacency Matrix Example For undirected graph, what would adjacency matrix look like? Label Index A 0 B 1 C 2 D 3 E 4 mapping of vertex labels to array indices B D A E C In a weighted graph, cells would contain weights 0 1 2 3 4 0 0 1 0 1 0 1 0 0 0 0 1 2 1 0 0 0 0 3 0 1 0 0 0 4 0 0 1 0 0 Adjacency Matrix: array of edges indexed by vertex number CS200 - Graphs 33

Question Is this an undirected graph? A. Yes B. No 0 1 2 3 4 0 0 1 1 0 0 1 1 0 0 1 1 2 1 0 0 0 1 3 0 1 0 0 0 4 0 1 1 0 0 Adjacency Matrix: CS200 - Graphs 34

Question Is this a simple graph? A. Yes B. No 0 1 2 3 4 0 0 1 1 0 0 1 1 0 0 1 1 2 1 0 0 0 1 3 0 1 0 1 0 4 0 1 1 0 0 Adjacency Matrix: CS200 - Graphs 35

Graph Data Structures - Adjacency List n Vertices q mapped to list of adjacencies q adjacency: edge n Edges: lists of adjacencies B A C q linked-list of out-going edges per vertex n useful for sparse graphs D E CS200 - Graphs 36

Adjacency List: Undirected Graph ArrayList A Index Label ArrayLists B C 0 A B C D 1 B A D E D E 2 C 3 D A A E B 4 E B C mapping of vertex labels to list of edges CS200 - Graphs 37

Adjacency List: Directed Graph ArrayList A Index Label ArrayLists B C 0 A B D 1 B E D E 2 C 3 D 4 E A B C This representation is used in Graph recitation and assignment CS200 - Graphs 38

Which Implementation Is Best? n Which implementation best supports common Graph Operations: q Is there an edge between vertex i and vertex j? q Find all vertices adjacent to vertex j n Which best uses space? CS200 - Graphs 39

Shortest Path Algorithms (Dijkstra s Algorithm) n Graph G(V,E) with positive weights ( distances ) n Compute shortest distances from source vertex s to every other vertex in the graph n Examples: Google/Apple/Navigator maps, network routing, laying out wires on a Printed Circuit Board (PCB), etc. CS200 - Graphs 40

Shortest Path Algorithms (Dijkstra s Algorithm) n Algorithm q Stepwise create a minimal path sub tree initial: source q Maintain array d (minimum distance estimates) n n Init: d[s]=0, d[v]=, v V-s means: yet unreachable q array of nodes not yet visited with shortest distance to already selected nodes q select minimum path distance node v, update neighbors CS200 - Graphs 41

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 an empty set, call it finalizedvertexset 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 finalizedvertexset add v to finalizedvertexset for (all vertices u not in finalizedvertexset) if (d[u] > d[v] + w[v][u]) d[u] = d[v] + w[v][u] CS200 - Graphs 42

0 s b c a 2 7 1 5 2 1 b 0 s 2 7 c a 2 7 1 5 2 1 0 s 2 b 3 c 7 a 2 7 1 5 2 1 0 s 2 b 3 c a 2 7 1 5 2 1 4 Do It Do It

Recap: Priority Queue n A Priority Queue is a data structure that keeps a set of items (P,V), consisting of a Priority P and a Value V, in sorted order of priority. n Possible operations: insert( X(P x,v x ) ) delete( X(P x,v x ) ) We have studied a clever data structure for priority queues: heaps CS200 - Graphs 44

Shortest Path Algorithms Using a Priority Queue (Dijkstra s Algorithm): step 1 b 10 1 8 e 10 a 0 3 2 9 4 6 5 7 [5,c],[10,b] c 5 2 8 d CS200 - Graphs 45

Shortest Path Algorithms (Dijkstra s Algorithm): step 2 10 b 1 8 14 e a 0 3 2 9 4 6 5 7 [7,d],[8,b],[14,e] c 5 7 2 d CS200 - Graphs 46

Shortest Path Algorithms (Dijkstra s Algorithm): step 3 10 b 1 8 11 e a 0 3 2 9 4 6 5 [8,b],[11,e] c 5 7 2 7 d CS200 - Graphs 47

Shortest Path Algorithms (Dijkstra s Algorithm): step 4 10 b 1 8 9 e a 0 3 2 9 4 6 5 7 [9,e] c 5 7 2 d CS200 - Graphs 48

Shortest Path Algorithms (Dijkstra s Algorithm): Done 10 b 1 8 9 e a 0 3 2 9 4 6 5 7 c 5 7 2 d CS200 - Graphs 49

Dijkstra s Algorithm n We have computed the shortest distances. n How to obtain the shortest paths? q At each vertex maintain predecessor on path (parent in the minimal path tree) q From each node you can trace back to the source (the root of the minimal path tree) q Why maintain predecessor, why not successor? CS200 - Graphs 50