(Re)Introduction to Graphs and Some Algorithms

Similar documents
CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

Trees. Arash Rafiey. 20 October, 2015

22.3 Depth First Search

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Algorithm Design and Analysis

Graph Representations and Traversal

22.1 Representations of graphs

Algorithm Design and Analysis

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

CSI 604 Elementary Graph Algorithms

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

W4231: Analysis of Algorithms

CS 310 Advanced Data Structures and Algorithms

Graph: representation and traversal

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Figure 1: A directed graph.

Graph. Vertex. edge. Directed Graph. Undirected Graph

Basic Graph Definitions

COT 6405 Introduction to Theory of Algorithms

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Chapter 22 Elementary Graph Algorithms

CSE 100: GRAPH ALGORITHMS

Graph Representation

Algorithm Design and Analysis

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CS Algorithms and Complexity

Some Extra Information on Graph Search

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

Copyright 2000, Kevin Wayne 1

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

Introduction to Graphs. common/notes/ppt/

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Data Structures and Algorithms. Chapter 7. Graphs

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

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Inf 2B: Graphs II - Applications of DFS

DFS & STRONGLY CONNECTED COMPONENTS

CS 206 Introduction to Computer Science II

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graphs. Motivations: o Networks o Social networks o Program testing o Job Assignment Examples: o Code graph:

3.1 Basic Definitions and Applications

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Elementary Graph Algorithms

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

Chapter 9 Graph Algorithms

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

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

Topic 12: Elementary Graph Algorithms

Elementary Graph Algorithms

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CS302 - Data Structures using C++

Graphs & Digraphs Tuesday, November 06, 2007

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

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

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

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

Design and Analysis of Algorithms

Design and Analysis of Algorithms

Outline. Graphs. Divide and Conquer.

Review: Graph Theory and Representation

Algorithm Design and Analysis

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

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

Basic Graph Algorithms (CLRS B.4-B.5, )

Konigsberg Bridge Problem

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Outlines: Graphs Part-2


Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Info 2950, Lecture 16

Solutions to relevant spring 2000 exam problems

Strongly Connected Components

Applications of BDF and DFS

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Trees Algorhyme by Radia Perlman

Design and Analysis of Algorithms

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

csci 210: Data Structures Graph Traversals

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Graph Traversals. CS200 - Graphs 1

Minimum Spanning Trees Ch 23 Traversing graphs

(Refer Slide Time: 02.06)

Breadth First Search. cse2011 section 13.3 of textbook

Strongly Connected Components. Andreas Klappenecker

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

Chapter 9 Graph Algorithms

Inf 2B: Graphs, BFS, DFS

Lecture 10: Strongly Connected Components, Biconnected Graphs

Transcription:

(Re)Introduction to Graphs and Some Algorithms

Graph Terminology (I) A graph is defined by a set of vertices V and a set of edges E. The edge set must work over the defined vertices in the vertex set. Many different types of relationships can be represented as graphs. Graphs (specifically edges) can be either directed (eg: driving on a street) or undirected (eg: walking on a street). If two vertices are connected by an edge, we say those vertices are adjacent to each other. Edges can have values associated with them, in which case we call the graph a weighted graph.

Graph Terminology (II) A path is a list of edges that are sequentially connected. The length of a path is the number of edges. We will say that vertex b is reachable from a if there is a path from a to b. A cycle is a path where the starting vertex is also the ending vertex. A Hamiltonian Path is a path that visits every vertex in a graph exactly once. An Eulerian Path is a path that visits every edge in a graph exactly once.

Graph Representation (Directed)

Graph Representation (Undirected)

A Proof on Graphs Definitions: In a directed graph, the in-degree of a vertex is the number of edges going into it and the outdegree of a vertex is the number of edges coming out of it. Theorem: Σ in-degree(v) = Σ out-degree(v) Proof will be by induction. Start with a base case of a graph with a single edge. For the inductive hypothesis, say that for any graph with an edge set of size k that the theorem holds. Then show that it holds for any graph with an edge set of size k+1.

Inductive Hypothesis For any graph with edge set of size k (k 1) Σ in-degree(v) = Σ out-degree(v) Inductive Step Show that for any graph G with edge set of size k+1 that Σ in-degree(v) = Σ out-degree(v) Let H be a generic particular graph with k+1 edges. Select an edge (call it e 1 ) and remove it from the edge set to create a new graph H. By our definitions, Σ in-degree(v) = Σ in-degree(v) + 1 Σ out-degree(v) = Σ out-degree(v) + 1 Can now ask: Σ in-degree(v) + 1 = Σ out-degree(v) + 1

How can we solve Eulerian path? If all we want is a yes/no answer, it s fairly easy. If we also want to find the actual path if it exists, that becomes a much more involved question For one point, we need to think about algorithms that are able to traverse graphs. So, let s look at one

Breadth-First Search Given a graph, one way to have an algorithm try to visit every vertex in that graph is via a breadth-first search. Select a starting point. Visit all vertices that are one jump away from it. Visit all vertices that are two jumps away from it. etc. What if the graph is directed? If the graph is not connected, what ends up happening? A simple problem that can be solved using this general technique is that of finding the shortest path between two vertices in an undirected and unweighted graph.

Shortest Path via BFS Starting at vertex s V generate an array of distances from s called dist[] such that v V, dist[v]=length of shortest path from s to v. dist[s]=0 We will also create a predecessor array of the last vertex we were at before getting to the end of the path from s to v v V, pred[v]= one step back pred[s]=none With just these two arrays, we will be able to reconstruct any shortest part request from s to some vertex. This is because any sub-path of the optimal path must also be an optimal path between its own endpoints. If it weren t, then we could have replaced it and gotten a shorter overall path.

Start at s. Basic Pseudocode For each neighbor v of s dist[v]=1 pred[v]=s. Move outwards from each neighbor you ve seen and set the next ripple out as +1 of the current distance, and set pred[] appropriately. Need a way to make sure we don t end up in cycles!

Avoiding Cycles We will assign a color to each vertex based on the following rules: - white = not seen yet at all - gray = seen but not processed yet - black = processed We will create a queue of gray vertices, and will never add any vertex to the queue more than once. When we are done processing a vertex (ie: we have touched all its neighbors) we go back to the queue to get the next vertex to process.

More Detailed Pseudocode BFS (Graph G, vertex s) { int size = G.getVertexCount; int dist = new int[size]; vertex pred = new int[size]; Queue Q= new Queue<vertex>; Colors state = new Colors[size]; for each v in G.V { state[v]=white; dist[v]=infinity; pred[v]=none; } } state[s]=gray; dist[s]=0; pred[s]=none; Q.add(s); while (!Q.empty()){ u=q.remove(); for each unvisited v in G.Adj(u) { state[v]=gray; dist[v]=dist[u]+1; pred[v]=u; Q.add(v); } state[u]=black; }

What s the runtime? Each vertex gets enqueued at most one time, so each is processed at most one time. Write this up using a summation to represent the processing of all of the vertices Our runtime will be order: V for all of the initializations The while loop s cost can be seen as the sum across all vertices u in V of: - the degree(u) for work inside the for loop - +1 for the work outside of the for loop We can split the summation into two simpler ones and if you work it through, the runtime is O( V + E ).

What else does BFS give us? It allows us to organize the entire graph as ripples away from a central point. This could be useful if we could restate other questions within this framework. Our predecessor array could be used to create a tree rooted at source s of vertices that can be reached from s. This is often called a breadth-first tree. If we could phrase a problem as a traversal of this tree

Depth-First Search You could basically just change the Queue in the BFS code into a Stack. You could also just write it out as a recursive algorithm. This approach can also be used to determine what vertices are reachable in O( E + V ) time.

DFS on a Directed Graph with Timing Info We can add more arrays and store information such as when (in terms of a continuously advancing ticker) each vertex is first visited and finally processed. Even in a connected graph, we might end up having to build a forest of trees to give every vertex a set of times. After doing a DFS from a given starting point, if there are vertices with no times, choose one of them, and continue.

Example Graph

Topological Sort of a Digraph NOTE: This only works if there are no cycles, since if there are cycles there isn t the notion of a sorted order. Imagine a graph as beads where the edges are strings of equal length connecting ordered pairs of beads. You want to arrange the beads so that all edges point left-to-right. How can you use a DFS with timing info to accomplish this?

Strongly Connected Components We define strongly connected to mean that for every pair of vertices (u,v) in the component, there is a path from u to v and from v to u. In the following graph, what are the strongly connected components?

Finding the SCCs Step 1: Perform a DFS with timing on the graph G. Step 2: Perform a DFS with timing on the graph G T with the added restriction that when you have a choice of vertices, you choose the one with the largest finish time from Step 1 s search. Every time your algorithm hits a deadend, you have finished one strongly connected component and are ready to start finding the next one. Let s trace this on the graph from the previous slide

Could you use a BFS or DFS to Detect whether a given graph has any cycles? Yes. Determine whether every vertex is reachable from a particular vertex in a given graph? Yes. Find the longest simple path through a graph between two vertices in an unweighted graph that might contain cycles? No!