CMSC 341 Graphs.

Slides:



Advertisements
Similar presentations
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Advertisements

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.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
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 Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
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.
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
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.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
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 Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Upon completion you will be able to:
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.
CMSC 341 Graphs – DFS Expanded. 2 Depth First Traversal with Finish Times dfs(Graph G) { for (each v  V) d[v] = 0// d = discovery “time” time = 0// “global”
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.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CMSC 341 Graphs. 5/5/20062 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge.
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.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CS202 - Fundamental Structures of Computer Science II
Elementary Graph Algorithms
CS212: Data Structures and Algorithms
Data Structures 13th Week
CMSC 341 Graphs.
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
Introduction to Graphs
Depth-First Search.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graph Representation, DFS and BFS
Graphs Graph transversals.
CMSC 341 Graphs.
Graph & BFS.
Graph Representation (23.1/22.1)
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.
CMSC 341 Lecture 20.
Connected Components, Directed Graphs, Topological Sort
Graphs G = (V, E) V are the vertices; E are the edges.
CE 221 Data Structures and Algorithms
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Data Structures and Algorithm Analysis Graph Algorithms
CMSC 341 Graphs 2.
CE 221 Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Elementary Graph Algorithms
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:

CMSC 341 Graphs

Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w) where v, w  V. V and E are sets, so each vertex v  V is unique, and each edge e  E is unique. Edges are sometimes called arcs or lines. Vertices are sometimes called nodes or points. Show examples of directed graph undirected graph

Basic Graph Definitions (cont’d) A directed graph is a graph in which the edges are ordered pairs. That is, (u,v)  (v,u), u, v  V. Directed graphs are sometimes called digraphs. An undirected graph is a graph in which the edges are unordered pairs. That is, (u,v) = (v,u).

Basic Graph Examples a b e c d a b e c d undirected graph directed graph

Basic Graph Definitions (cont’d) Vertex v is adjacent to vertex w if and only if (v,w)  E. (Book calls this adjacent from) Vertex v is adjacent from vertex w if and only if (w,v)  E. An edge may also have: weight or cost -- an associated value label -- a unique name The degree of a vertex u in an undirected graph is the number of vertices adjacent to u. Degree is also called valence. The indegree (outdegree) of a vertex u in a directed graph is the number of vertices adjacent to (from) u. Does it matter in an undirected graph? No. Give examples: adjacent to (in undirected: a is adjacent to b but not d) adjacent from (in directed: a is adjacent to b, adjacent from e, but neither adjacent to/from d) degree (in undirected: a has degree 2) indegree (in directed: a has indegree 1 and b has indegree 1) outdegree (in directed: a has outdegree 1, b has outdegree 1

Paths in Graphs A path in a graph is a sequence of vertices w1, w2, w3, …, wn such that (wi, wi+1)  E for 1  i < n. The length of a path in a graph is the number of edges on the path. The length of the path from a vertex to itself is 0. A simple path is a path such that all vertices are distinct, except that the first and last may be the same. A cycle in a graph is a path w1, w2, w3, …, wn , w  V such that: there are at least two vertices on the path w1 = wn (the path starts and ends on the same vertex) if any part of the path contains the subpath wi, wj, wi, then each of the edges in the subpath is distinct. A simple cycle is one in which the path is simple. Example: simple path (in undirected: from a to d there are two simple paths of length 2 (abd and aed) and a path of length 3 (abcd) (in undirected: from a to d there is one simple path (abd) simple cycle(in undirected: abdea, bdcb. The cycle abcdba is not simple) (in directed: abdea bdcb. The cycle vdcbdeab is not simple)

Connectedness in Graphs An undirected graph is connected if there is a path from every vertex to every other vertex. A directed graph is strongly connected if there is a path from every vertex to every other vertex. A directed graph is weakly connected if there would be a path from every vertex to every other vertex, disregarding the direction of the edges. A complete graph is one in which there is an edge between every pair of vertices. A connected component of a graph is any maximal connected subgraph. Connected components are sometimes simply called components.

a b g c f h d e j i Is it strongly connected? -- NO Strongly connected components: g hij bacf d e Weakly connected component: whole graph e j i

A Graph ADT Has some data elements Has some operations vertices edges getDegree(u) -- returns the degree of vertex u (undirected graph) getInDegree(u) -- returns the indegree of vertex u (directed graph) getOutDegree(u) -- returns the outdegree of veretx u (directed graph) getAdjacent(u) -- returns a list of the vertices adjacent from a vertex u (directed and undirected graphs) isConnected(u,v) -- returns TRUE if vertices u and v are connected, FALSE otherwise (directed and undirected graphs)

Graph Traversals Like trees, can be traversed breadth-first or depth-first. Use stack for depth-first traversal. Use queue for breadth-first traversal. Unlike trees, need to specifically guard against repeating a path from a cycle. Can mark each vertex as “visited” when we encounter it and not consider visited vertices more than once.

Breadth-First Traversal Queue q = new Queue(); graphvertex u; for all v, d[v] =  // mark each vertex unvisited q.enqueue(startvertex); // start with any vertex d[startvertex] = 0; // mark visited while (!q.isEmpty()) { u = q.dequeue(); for (each vertex w adjacent from u) if (d[w] == ) { // w not marked as visited d[w] = d[u]+1; // mark visited q.enqueue(w); }

v3 v1 v2 v5 v4 Traversal 0: marks all unvisited 1: marks v1 as visited 2. Enqueue and mark v2, v3 3. Dequeue v2, enqueue and mark v4 4. Dequeue v3 -- no op 5. Dequeue v4 -- no op BFS order: 1 2 3 4 Is v1 reachable from v2 ? Yes if v2 does not end up with  mark after BFS from v1 What are minimum number of vertices that must be visited on a path from v1 to vk? D[vk] v4

Depth First Traversal dfs(Graph G) { for (each v  V) dfs(v) } dfs(Vertex v) { markVisited(v); for(each vertex w adjacent from u) if ( w is not marked as visited) dfs(w)

Depth First Traversal with Finish Times dfs(Graph G) { for (each v  V) d[v] = 0 // d = discovery “time” time = 0 // “global” variable if (d[v] = 0) // not discovered yet dfs (v) } dfs(Vertex v) { time = time + 1 d[v] = time // “discover” and mark v for(each vertex w adjacent from v) if (d[w] = 0) // w not discoverd dfs(w) f[v] = time // v is “finished”

v3 v1 v2 v5 Traversal 0: 1: 2. 3. 4. 5. DFS order: 1 2 4 3 v4

DFS (stack version) Stack s = new Stack(); GraphVertex u; GraphVertex startvertex = graph.getStartVertex(); s.push(startvertex); markVisited(startvertex); while (!s.isEmpty()) { u = s.Pop(); for (each vertex w adjacent to u) if (w is not marked as visited) { markVisited(w); s.push(w); } Edges traversed form a tree which includes all visited vertices in the graph. This is called a spanning tree.

Unweighted Shortest Path Problem Unweighted shortest-path problem: Given as input an unweighted graph, G = (V,E), and a distinguished vertex, s, find the shortest unweighted path from s to every other vertex in G. After running BFS algorithm with s as starting vertex, the shortest path length from s to i is given by d[i].