Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
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.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
Chapter 9: Graphs Basic Concepts
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
CSC 413/513: Intro to Algorithms Graph Algorithms.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
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.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I.
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.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
CS 201: Design and Analysis of Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
Introduction to Graphs
Data Structures, Algorithms & Complexity
CSC317 Graph algorithms Why bother?
Introduction to Graphs
Graphs Breadth First Search & Depth First Search
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graph.
Graph Representation, DFS and BFS
Elementary Graph Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Chapter 9: Graphs Basic Concepts
Graphs.
Basic Graph Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II
Chapter 9: Graphs Basic Concepts
GRAPHS.
Introduction to Graphs
For Friday Read chapter 9, sections 2-3 No homework
Introduction to 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:

Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I

Objects and Connections Many problems are naturally formulated in terms of objects and the connections between them. For example: Airline Routes--What is the fastest (or cheapest) way to get from one city to another? Electrical circuits--Circuit elements are wired together. How does the current flow? Job Scheduling--The objects are tasks that need to be performed. The connections indicate which jobs should be done before which other jobs. Web site design--How are the pages in the site connected? A graph is a mathematical object that describes such situations. Algorithms for graphs are fundamental to CS Graph Theory is a major branch of combinatorial mathematics.

Graphs A graph is a collection of vertices, V, and edges, E. An edge connects two vertices. a d a is the same as: d b c b c Vertices: a, b, c, d Edges: ab, bc, ac, ad

Definitions A path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph. a d Example: A path from a to c could be ac or abc. b c A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected. a d Not Connected e b c

More definitions A simple path is a path with no vertex repeated. A simple cycle is a simple path except the first and last vertex is repeated and, for an undirected graph, number of vertices >= 3. Example: abca is a cycle a d b c A tree is a graph with no cycles. a d b c

Definitions Continued A complete graph is a graph in which all edges are present. A sparse graph is a graph with relatively few edges. A dense graph is a graph with many edges. a d a d a d b c b c b c Complete Dense Sparse

Types of Graphs An undirected graph has no specific direction between the vertices. A directed graph has edges that are "one way". We can go from one vertex to another, but not vice versa. A weighted graph has weights associated with each edge. The weights can represent distances, costs, etc. 34 a d a d a d 22 4 13 19 b c b c b c Undirected Directed Weighted

Representing Graphs as an Adjacency List An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 1 2 1 3 2 5 4 3 4 5

Representing a Graph with an Adjacency Matrix An adjacency matrix is a matrix with a row and column for each vertex. The matrix entry is 1 if there is an edge between the row vertex and the column vertex. 1 2 1 2 3 4 5 1 2 3 4 5 3 5 4 The diagonal may be zero or one. Each edge is represented twice.

Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 1 2 4 5 6 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6

Representing Weighted Graphs In weighted graphs we include the weights of each edge. 2 List 1 2 3 1 1 3 6 5 7 2 4 4 5 6 1 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6

Lists vs. Matrices Speed: An adjacency matrix provides the fastest way to determine whether or not a particular edge is present in the graph. For an adjacency list, there is no faster way than to search the entire list for the presence of the edge. Memory: Normally, an adjacency matrix requires more space (n2 entries). However, if n is small and the graph is unweighted, an adjacency matrix only needs 1 bit per entry. The adjacency list requires at least 1 word per entry (for the address of the next node of the list). This may offset the advantage of fewer entries.

Breadth First Search (BFS) Problem: Given a graph, G = (V, E), find all the vertices reachable from some source, s. Compute the distance (shortest path) from s to each vertex. Produce a tree such that: s is the root of the tree The nodes of the tree represent all reachable vertices The path from s to v corresponds to the path containing the fewest edges from s to v in the graph.

Strategy for BFS As we search the graph we: Keep track of "discovered" and "undiscovered" vertices. Maintain a frontier of discovered vertices that spreads out across the graph. Use three colors: White = undiscovered vertex Gray= Discovered vertex, but we don't know all its connections Black= Discovered vertex and we know all its connections.

Strategy continued For each gray vertex starting with s: Find all undiscovered vertices reachable by 1 edge from that vertex and color them gray. Add them to the tree below v. Color the original vertex black. Process all vertices at level k (distance k from s) before processing the vertices at level k+1

Pseudocode for BFS BFS(G, s) //G is graph, s is source vertex for each vertex u in G, V – {s} //Initialize each vertex to white u.color = white u.dist = ∞ u.pred = NIL s.color = gray //s is discovered s.dist = 0 s.pred = NIL Q = {} Q = Enqueue(Q, s) //Start with s in the queue while Q is not empty u = Dequeue(Q) //Process next element for each v in G.Adj[u] //Check all adjacent vertices if v.color == white //If undiscovered discover it v.color = gray v.dist = u.dist + 1 v.pred = u Enqueue(Q, v) //Add it to the queue u.color = black //Once adjacent vertices discovered, color this one black

Example r s t u v w x y We will work through this in class.

Analysis Initialization: Q(V) While loop: Each vertex enqueued and dequeued once: Q(V) Adjacency list scanned once for each vertex. Each edge is represented twice in all lists. Sum of the lengths of all lists = 2E, so The time to process the adjacency lists will be Q(E) Total for while loop = Q(V) + Q(E) = Q(V + E) Total = Q(V) + Q(V+E) = Q(V+E)