CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Similar documents
CS/COE

Undirected Graphs. Hwansoo Han

Elementary Graph Algorithms CSE 6331

Graph Algorithms Using Depth First Search

Lecture 9 Graph Traversal

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph Algorithms. Definition

Graphs & Digraphs Tuesday, November 06, 2007

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Trees. Arash Rafiey. 20 October, 2015

CS 310 Advanced Data Structures and Algorithms

Algorithm Design and Analysis

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

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

Konigsberg Bridge Problem

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

CS302 - Data Structures using C++

Fundamental Algorithms

Announcements Problem Set 4 is out!

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Searching in Graphs (cut points)

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

Lecture 3: Graphs and flows

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017

CSE 100: GRAPH ALGORITHMS

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

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

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

Algorithm Design and Analysis

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

CSC263 Week 8. Larry Zhang.

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Data Structures Brett Bernstein

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

Graph Search. Adnan Aziz

CS4800: Algorithms & Data Jonathan Ullman

國立清華大學電機工程學系. Outline

March 20/2003 Jayakanth Srinivasan,

GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms?

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

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

Graph Biconnectivity

COT 6405 Introduction to Theory of Algorithms

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Lecture 26: Graphs: Traversal (Part 1)

Outline. Graphs. Divide and Conquer.

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

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

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

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

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

TIE Graph algorithms

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

Elementary Graph Algorithms

TIE Graph algorithms

Representations of Graphs

CS 4407 Algorithms Lecture 5: Graphs an Introduction

Graphs. Data Structures 1 Graphs

CSE 373: Data Structures and Algorithms

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Info 2950, Lecture 16

Algorithms: Lecture 10. Chalmers University of Technology

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

Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions.

MAL 376: Graph Algorithms I Semester Lecture 1: July 24

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

Brute Force: Selection Sort

Algorithms Sequential and Parallel: A Unified Approach; R. Miller and L. Boxer 3rd Graph Algorithms

Graph Representation

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Parallel Pointers: Graphs

Introduction to Computer Science and Programming for Astronomers

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

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

Algorithm Design and Analysis

Inf 2B: Graphs, BFS, DFS

Topic 12: Elementary Graph Algorithms

CS Algorithms and Complexity

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

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

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

Solutions to Midterm 2 - Monday, July 11th, 2009

Applications of BDF and DFS

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Copyright 2000, Kevin Wayne 1

ECE 242. Data Structures

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

Transcription:

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

5 3 2 4 1 0 2

Graphs A graph G = (V, E) Where V is a set of vertices E is a set of edges connecting vertex pairs Example: V = {0, 1, 2, 3, 4, 5} E = {(0, 1), (0, 4), (1, 2), (1, 4), (2, 3), (3, 4), (3, 5)} 5 3 4 0 2 1 3

Why? Can be used to model many different scenarios 4

Some definitions Undirected graph Edges are unordered pairs: (A, B) == (B, A) Directed graph Edges are ordered pairs: (A, B)!= (B, A) Adjacent vertices, or neighbors Vertices connected by an edge 5

Graph sizes Let v = V, and e = E Given v, what are the minimum/maximum sizes of e? Minimum value of e? Definition doesn t necessitate that there are any edges So, 0 Maximum of e? Depends Are self edges allowed? Directed graph or undirected graph? In this class, we ll assume directed graphs have self edges while undirected graphs do not 6

More definitions A graph is considered sparse if: e <= v lg v A graph is considered dense as it approaches the maximum number of edges i.e., e == MAX - ε A complete graph has the maximum number of edges 7

Question: 5 5 3 4 = 4 3 0 or 0 2 1 1 2? 8

Related: = or? 9

Representing graphs Trivially, graphs can be represented as: List of vertices List of edges Performance? Assume we re going to be analyzing static graphs i.e., no insert and remove So what operations should we consider? 10

Using an adjacency matrix Rows/columns are vertex labels M[i][j] = 1 if (i, j) E M[i][j] = 0 if (i, j) E 0 1 2 3 4 5 0 0 1 0 0 1 0 1 1 0 1 0 1 0 2 0 1 0 1 0 0 3 0 0 1 0 1 1 4 1 1 0 1 0 0 5 0 0 0 1 0 0 11

Adjacency matrix analysis Runtime? Space? 12

Adjacency lists Array of neighbor lists A[i] contains a list of the neighbors of vertex i 0 1 2 3 4 5 1 0 1 2 0 3 4 2 3 4 1 4 5 3 13

Adjacency list analysis Runtime? Space? 14

Comparison Where would we want to use adjacency lists vs adjacency matrices? What about the list of nodes/list of edges approach? 15

Even more definitions Path A sequence of adjacent vertices Simple Path A path in which no vertices are repeated Simple Cycle A simple path with the same first and last vertex Connected Graph A graph in which a path exists between all vertex pairs Connected Component Connected subgraph of a graph Acyclic Graph A graph with no cycles Tree? A connected, acyclic graph (often rooted ) Has exactly v-1 edges 16

Graph traversal What is the best order to traverse a graph? Two primary approaches: Depth-first search (DFS) Dive as deep as possible into the graph first Branch when necessary Breadth-first search (BFS) Search all directions evenly i.e., from i, visit all of i s neighbors, then all of their neighbors, etc. 17

DFS Already seen and used this throughout the term For tries For Huffman encoding Can be easily implemented recursively For each node, visit first unseen neighbor Backtrack at dead ends (i.e., nodes with no unseen neighbors) Try next unseen neighbor after backtracking 18

DFS example 5 3 2 4 1 0 19

DFS example 2 0 1 2 3 6 4 5 7 9 8 20

BFS Can be easily implemented using a queue For each node visited, add all of its neighbors to the queue Vertices that have been seen but not yet visited are said to be the fringe Pop head of the queue to be the next visited vertex See example 21

BFS example 0 Q 1 7 1 2 2 9 3 6 3 8 4 5 4 7 9 5 8 6 22

Shortest paths BFS traversals can further be used to determine the shortest path between two vertices 23

Analysis of graph traversals At a high level, DFS and BFS have the same runtime Each node must be seen and then visited, but the order will differ between these two approaches How will the representation of the graph affect the runtimes of of these traversal algorithms? 24

DFS and BFS would be called from a wrapper function If the graph is connected: dfs()/bfs() is called only once and returns a spanning tree Else: A loop in the wrapper function will have to continually call dfs()/bfs() while there are still unseen vertices Each call will yield a spanning tree for a connected component of the graph 25

DFS pre-order traversal 26

DFS in-order traversal 27

DFS post-order traversal 28

Biconnected graphs A biconnected graph has at least 2 distinct paths (no common edges or vertices) between all vertex pairs Any graph that is not biconnected has one or more articulation points Vertices, that, if removed, will separate the graph Any graph that has no articulation points is biconnected Thus we can determine that a graph is biconnected if we look for, but do not find any articulation points 29

Finding articulation points Variation on DFS Consider building up the spanning tree Have it be directed Create back edges when considering a node that has already been visited in constructing the spanning tree Label each vertex v with with two numbers: num(v) = pre-order traversal order low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge Min of: num(v) Lowest num(w) of all back edges (v, w) Lowest low(w) of all spanning tree edges (v, w) 30

Finding articulation points example F num 0 E 0 low D E A 1 A 0 C B 2 B 0 3 C 0 4 D 0 5 F 5 31

So where are the articulation points? If any (non-root) vertex v has some child w such that low(w) num(v), v is an articulation point What about if we start at an articulation point? If the root of the spanning tree has more than one child, it is an articulation point 32