Fundamental Algorithms

Similar documents
Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

CSE 100: GRAPH ALGORITHMS

COMP 251 Winter 2017 Online quizzes with answers

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

Algorithm Design and Analysis

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

Graph traversal is a generalization of tree traversal except we have to keep track of the visited vertices.

Graph Algorithms Using Depth First Search

Trees. Arash Rafiey. 20 October, 2015

Algorithm Design (8) Graph Algorithms 1/2

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

Unweighted Graphs & Algorithms

csci 210: Data Structures Graph Traversals

Graph Algorithms. Definition

12 Abstract Data Types

CS/COE

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

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

Algorithm Design and Analysis

Undirected Graphs. Hwansoo Han

Data Structure. IBPS SO (IT- Officer) Exam 2017

Graph. Vertex. edge. Directed Graph. Undirected Graph

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

implementing the breadth-first search algorithm implementing the depth-first search algorithm

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

CSI 604 Elementary Graph Algorithms

W4231: Analysis of Algorithms

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

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

1. Graph and Representation

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

Parallel Pointers: Graphs

TIE Graph algorithms

Announcements Problem Set 4 is out!

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

22.1 Representations of graphs

LECTURE 17 GRAPH TRAVERSALS

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

Applications of BDF and DFS

csci 210: Data Structures Graph Traversals

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

CS4800: Algorithms & Data Jonathan Ullman

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

Brute Force: Selection Sort

Lecture 9 Graph Traversal

CS 310 Advanced Data Structures and Algorithms


Basic Graph Definitions

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

Module 11: Additional Topics Graph Theory and Applications

TIE Graph algorithms

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

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

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

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Graph: representation and traversal

Algorithm Design and Analysis

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

Topic 12: Elementary Graph Algorithms

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

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

CS 206 Introduction to Computer Science II

Graph Search. CS/ECE 374: Algorithms & Models of Computation, Fall Lecture 15. October 18, 2018

Module 5 Graph Algorithms

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CS302 - Data Structures using C++

Data Structures Question Bank Multiple Choice

Lecture 10 Graph algorithms: testing graph properties

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Graphs: basic concepts and algorithms

CSE 613: Parallel Programming. Lecture 11 ( Graph Algorithms: Connected Components )

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.

Implementing Algorithms

Graph Search. Algorithms & Models of Computation CS/ECE 374, Fall Lecture 15. Thursday, October 19, 2017

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Breadth First search and Depth First search Finding connected components

(Re)Introduction to Graphs and Some Algorithms

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

Konigsberg Bridge Problem

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Lecture 3: Graphs and flows

Review: Graph Theory and Representation

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

( ) n 3. n 2 ( ) D. Ο

Copyright 2000, Kevin Wayne 1

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

Shortest path problems

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Algorithms: Lecture 10. Chalmers University of Technology

Design and Analysis of Algorithms

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

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.

Graph Representations and Traversal

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

GRAPHS Lecture 17 CS2110 Spring 2014

Outline. Graphs. Divide and Conquer.

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Transcription:

Fundamental Algorithms Chapter 8: Graphs Jan Křetínský Winter 2017/18 Chapter 8: Graphs, Winter 2017/18 1

Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set E of edges between the vertices. undirected graph: (i, j) E an unordered pair (i, j) = (j, i) directed graph (or shorter: digraph ): (i, j) E an ordered tuple, i.e. (i, j) E independent of (j, i) E Chapter 8: Graphs, Winter 2017/18 2

Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set E of edges between the vertices. undirected graph: (i, j) E an unordered pair (i, j) = (j, i) directed graph (or shorter: digraph ): (i, j) E an ordered tuple, i.e. (i, j) E independent of (j, i) E Some Terms two vertices V 0 and V n are connected by a path (of length n), if there is a sequence of edges (V 0, V 1 ), (V 1, V 2 ),..., (V n 1, V n ) a graph is connected, if there is a path between any two vertices a vertex V has degree d, if V has d (outgoing) edges Chapter 8: Graphs, Winter 2017/18 2

Graphs in CSE Unstructured Grids: in blue: V = grid cells, E = neighbours ( dual graph ) in black: V = grid vertices, E = cell edges Chapter 8: Graphs, Winter 2017/18 3

Trees Definition (Tree) A tree is a connected graph without cycles. Chapter 8: Graphs, Winter 2017/18 4

Trees Definition (Tree) A tree is a connected graph without cycles. Question: is this consistent with our naive image of a tree? Chapter 8: Graphs, Winter 2017/18 4

Trees Definition (Tree) A tree is a connected graph without cycles. Question: is this consistent with our naive image of a tree? Theorem A graph T is a tree, if and only if there is a unique path between any two distinct vertices of T. Chapter 8: Graphs, Winter 2017/18 4

Trees Definition (Tree) A tree is a connected graph without cycles. Question: is this consistent with our naive image of a tree? Theorem A graph T is a tree, if and only if there is a unique path between any two distinct vertices of T. Implications: there is only one connection from the root to any of the nodes any path between two nodes will run through the root of the resp. subtree actually: which node is the root? Chapter 8: Graphs, Winter 2017/18 4

Trees (2) Theorem A connected graph (V, E) is a tree, if and only if E = V 1 Chapter 8: Graphs, Winter 2017/18 5

Trees (2) Theorem A connected graph (V, E) is a tree, if and only if E = V 1 Implications: if you cut one edge, a tree is no longer connected (child becomes an orphan) building a tree incrementally requires a root (one node, no edge) and one additional edge per added node Chapter 8: Graphs, Winter 2017/18 5

Trees (2) Theorem A connected graph (V, E) is a tree, if and only if E = V 1 Implications: if you cut one edge, a tree is no longer connected (child becomes an orphan) building a tree incrementally requires a root (one node, no edge) and one additional edge per added node Definition (Spanning Tree) T = (V, E) is called a spanning tree for the graph G = (V, E ), if T is a tree, and E E. Note: T has the same vertices as G. Chapter 8: Graphs, Winter 2017/18 5

Data Structures for Graphs Pointer-Based Data Structure: (esp. for directed graphs) Node := ( key : Integer, edges : L i s t of Node ) ; } Chapter 8: Graphs, Winter 2017/18 6

Data Structures for Graphs Pointer-Based Data Structure: (esp. for directed graphs) Node := ( key : Integer, edges : L i s t of Node ) ; } Adjacency Matrix: n n matrix A, where n = V a ij = 1, if (i, j) E A is symmetric for undirected graphs Chapter 8: Graphs, Winter 2017/18 6

Data Structures for Graphs Pointer-Based Data Structure: (esp. for directed graphs) Node := ( key : Integer, edges : L i s t of Node ) ; } Adjacency Matrix: n n matrix A, where n = V a ij = 1, if (i, j) E A is symmetric for undirected graphs Note: to store an adjacency matrix as an n n array is a good idea, only if E Θ(n 2 ) Chapter 8: Graphs, Winter 2017/18 6

Graph Traversals Definition (Graph Traversal:) Input: a (connected!) directed or undirected graph (V, E), and a node x V. Task: Starting from x, visit all vertices in V (following edges only) Chapter 8: Graphs, Winter 2017/18 7

Graph Traversals Definition (Graph Traversal:) Input: a (connected!) directed or undirected graph (V, E), and a node x V. Task: Starting from x, visit all vertices in V (following edges only) Examples: modify the key values of all vertices search a specific key value in a graph Chapter 8: Graphs, Winter 2017/18 7

Graph Traversals Definition (Graph Traversal:) Input: a (connected!) directed or undirected graph (V, E), and a node x V. Task: Starting from x, visit all vertices in V (following edges only) Examples: modify the key values of all vertices search a specific key value in a graph Two main variants: depth-first traversal (depth-first search) breadth-first traversal (breadth-first search) Chapter 8: Graphs, Winter 2017/18 7

Depth-First Traversal DFTraversal (V : Node ) {! mark c u r r e n t node V as v i s i t e d : Mark [ V. key ] = 1;! perform desired work on V : V i s i t (V ) ;! perform t r a v e r s a l from a l l nodes connected to V f o r a l l (V,W) in V. edges do i f Mark [W. key ] = 0 then DFTraversal (W) ; end do ; } Assumptions: keys V.key numbered from 1,..., n = V Mark : Array[1..n] forall loop executed sequentially Chapter 8: Graphs, Winter 2017/18 8

DF-Traversal Stack-Based Implementation StackDFTrav (X : Node ) {! uses stack of a c t i v e nodes Stack a c t i v e = { X } ; Mark [ X. key ] = 1; while a c t i v e <> {} do! remove f i r s t node from stack V = pop ( a c t i v e ) ; V i s i t (V ) ; f o r a l l (V,W) in V. edges do i f Mark [W] = 0 then { push ( active, W) ; Mark [W. key ] = 1; } end do ; end while ; } use stack as last-in-first-out (LIFO) data container Chapter 8: Graphs, Winter 2017/18 9

Breadth-First-Traversal Queue-Based Implementation BFTraversal (X : Node ) {! uses queue of a c t i v e nodes Queue a c t i v e = { X } ; Mark [ X. key ] = 1; while a c t i v e <> {} do! remove f i r s t node from queue V = remove ( a c t i v e ) ; V i s i t (V ) ; f o r a l l (V,W) in V. edges do i f Mark [W. key ] = 0 then { append ( active, W) ; Mark [W. key ] = 1; } end do ; end while ; } use queue as first-in-first-out (FIFO) data container Chapter 8: Graphs, Winter 2017/18 10

Breadth-First Search BFSearch ( x : Node, k : Integer ) : Node { Queue a c t i v e = { x } ; while a c t i v e <> {} do V = remove ( a c t i v e ) ; i f V. key = k then return V ; i f Mark [ V. key ] = 0 then Mark [ V. key ] = 1 f o r a l l (V,W) in V. edges do append ( active, W) ; end do ; end i f ; end while ; } Chapter 8: Graphs, Winter 2017/18 11

Breadth-First Search BFSearch ( x : Node, k : Integer ) : Node { Queue a c t i v e = { x } ; while a c t i v e <> {} do V = remove ( a c t i v e ) ; i f V. key = k then return V ; i f Mark [ V. key ] = 0 then Mark [ V. key ] = 1 f o r a l l (V,W) in V. edges do append ( active, W) ; end do ; end i f ; end while ; } Breadth-First Search as Shortest-Path Algorithm: breadth-first search will return the node with the shortest path from x requires modification of algorithm to return this path, as well Chapter 8: Graphs, Winter 2017/18 11

Breadth-First and Depth-First Traversal DF/BF-Traversal and Connectivity of Graphs: DF- and BF-traversal will visit all nodes of a connected graph if a non-connected graph is traversed, both algorithms can be used to find the (maximum) connected sub-graph that contains the start node hence, DF- and BF-traversal can be extended to find all connectivity components of a graph Chapter 8: Graphs, Winter 2017/18 12

Breadth-First and Depth-First Traversal DF/BF-Traversal and Connectivity of Graphs: DF- and BF-traversal will visit all nodes of a connected graph if a non-connected graph is traversed, both algorithms can be used to find the (maximum) connected sub-graph that contains the start node hence, DF- and BF-traversal can be extended to find all connectivity components of a graph DF/BF-Traversal and Trees: DF- and BF-traversal will compute a spanning tree of a connected graph BF-traversal generates a spanning tree with shortest paths to the root Chapter 8: Graphs, Winter 2017/18 12