Graphs: basic concepts and algorithms

Similar documents
Elementary Graph Algorithms

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

22.3 Depth First Search

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

COT 6405 Introduction to Theory of Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph


Graph Algorithms Using Depth First Search

CSI 604 Elementary Graph Algorithms

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

CSC Intro to Intelligent Robotics, Spring Graphs

Algorithm Design and Analysis

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

Figure 1: A directed graph.

Basic Graph Algorithms

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Graph Algorithms. Definition

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

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

22.1 Representations of graphs

Outline. Graphs. Divide and Conquer.

W4231: Analysis of Algorithms

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

Homework No. 4 Answers

Graph and Digraph Glossary

CS 310 Advanced Data Structures and Algorithms

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

Topic 12: Elementary Graph Algorithms

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

Graphs & Digraphs Tuesday, November 06, 2007

March 20/2003 Jayakanth Srinivasan,

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Undirected Graphs. Hwansoo Han

Graph Representations and Traversal

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

Applications of BDF and DFS

Graph Representation

Lecture 9 Graph Traversal

COS 423 Lecture14. Robert E. Tarjan 2011

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

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

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

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

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

Chapter 22: Elementary Graph Algorithms. Definitions:

Data Structures and Algorithms. Chapter 7. Graphs

Lecture 3: Graphs and flows

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

Foundations of Discrete Mathematics

Chapter 9 Graph Algorithms

Algorithm Design (8) Graph Algorithms 1/2

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

csci 210: Data Structures Graph Traversals

Brute Force: Selection Sort

Konigsberg Bridge Problem

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

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

Graph Theory CS/Math231 Discrete Mathematics Spring2015

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

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

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

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS521 \ Notes for the Final Exam

Lecture 5: Graphs & their Representation

Chapter 9 Graph Algorithms

Lecture 10: Strongly Connected Components, Biconnected Graphs

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

Algorithm Design and Analysis

DFS & STRONGLY CONNECTED COMPONENTS

Fundamentals of Data Structure

Graph: representation and traversal

Chapter 11: Graphs and Trees. March 23, 2008

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Chapter 9 Graph Algorithms

Representations of Graphs

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Fundamental Algorithms

CS 4407 Algorithms Lecture 5: Graphs an Introduction

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

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

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

Algorithms: Lecture 10. Chalmers University of Technology

Graph implementations :

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

Elementary Graph Algorithms

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

Minimum Spanning Trees Ch 23 Traversing graphs

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

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

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

Data Structure and Algorithm, Spring 2017 Final Examination

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

Trees. Eric McCreath

Transcription:

: basic concepts and algorithms

Topics covered by this lecture: - Reminder Trees Trees (in-order,post-order,pre-order) s (BFS, DFS)

Denitions: Reminder Directed graph (digraph): G = (V, E), V - vertex (node) set, E V V - arc set (each arc is an ordered pair (u, v), where u, v V. (u - source, v - target of the arc) Undirected graph - the only dierence is that edge (undirected arc) (u, v) is an un-ordered pair, u, v V. Another variant: bidirected graphs. Self-loops usually not allowed. If multiple arcs(edges) possible - multi-graph. Generalisation: arc are not pairs, but n-tuples - hypergraph. e = (u, v) is incident to u, v, and u, v are adjacent. In-degree, out-degree of a node (directed); degree (undirected) sum of degrees is always even

Denitions: Reminder (2) Subgraph G G. Subgraph G induced by a subset of nodes V V : G = (V, E (V V )) Weights on arcs/edges (w : E R) Path (v 0,..., v k ) of length k. Simple path: nodes do not repeat. Cycle (path: v 0 == v k ). Hamiltonian cycle, Euler Cycle. Connected graph (there is a path between any pair of nodes), weakly connected: path can ignore the arc direction strongly connected component (SCC): a maximum strongly connected subgraph SCCs partition V (no intersections)

Number of edges if V == n and E = m then m = O(n 2 ) by graph size we usually mean m + n if m = o(n 2 ) the graph is called sparse full graph (has all possible edges): maximum number of edges Undirected full graph has exactly (n 1)n/2 edges empty graph - no edges (only nodes) how many edges a connected graph must have at least?

Number of edges if V == n and E = m then m = O(n 2 ) by graph size we usually mean m + n if m = o(n 2 ) the graph is called sparse full graph (has all possible edges): maximum number of edges Undirected full graph has exactly (n 1)n/2 edges empty graph - no edges (only nodes) how many edges a connected graph must have at least? n 1

Trees: Reminder Undirected tree: a connected graph without cycles (acyclic) undirected tree connected and has exactly (n 1) edges leaf, interior node Forest: an acyclic graph (not necessarily connected) rooted tree: ancestor, descendant, child, sibling, subtree, height (of tree or node): maximum distance to a leaf, depth (of node) distance to the root ordered tree (rooted tree with order on children) binary tree (ordered tree with max of 2 children of each node) DAG: Directed Acyclic

How to represent graphs in computer? Various graph representations are possible. The choice depends on which operations should be fast and how much memory is available. The most important operations on graphs: node/edge information access (weights, existence, etc.) navigation (typically: list of all outgoing arcs/edges) update (adding/removing nodes or edges/arcs) input/construction/conversion/output

Computer Representations of unordered sequence of edges (fast update, good as input/output format) adjacency matrices (extremely fast access, much memory, very slow extension; can be adapted to sparse graphs) adjacency arrays (good for static graphs) adjacency lists (least memory, easy update, relatively easy navigation) Except few cases (which?), translation from one representation to another is linear (fast).

Algebraic Theory Many interesting connections between linear algebra and graphs, for example: A - adjacency matrix: A k i,j == how many paths from i to j of length exactly k Algebraic Theory: studies such connections between matrices and graphs, etc.

Binary Tree A rooted tree - some node is distinguished and is called root. (On picture, the root is at the top). Case: complete tree. A binary tree is a rooted tree, and each node has maximum of 2 nodes, which are distinguishable (left and right). A binary tree can be represented as a linked structure: each node has links to its children the only access to the whole tree is a pointer to the root

Traversing Trees A general scheme: traverse(v): previsit(v) for each child w of v: traverse(w) postvisit(v) If postvisit is empty we call it pre-order, if previsit is empty post-order. Post-order can be used to compute height, pre-order for computing depth in trees.

In a special case of binary tree we have 3 important variants: in-order pre-order post-order

in-order order inordervisit(bintree currentnode){ if currentnode == null return inordervisit(currentnode.left) visit(currentnode) inordervisit(currentnode.right) }

pre-order order preordervisit(bintree currentnode){ if currentnode == null return visit(currentnode) preordervisit(currentnode.left) preordervisit(currentnode.right) }

post-order order postordervisit(bintree currentnode){ if currentnode == null return postordervisit(currentnode.left) postordervisit(currentnode.right) visit(currentnode) }

Example: expression trees Evaluate an expression: 2,+,3,/,6 A parser rst transforms it to an expression tree. The root is the last operator, numbers are in leaves, interior nodes are the other operators. Now, the evaluation is very easy: eval(r): if isleaf(r) return number(r) a = eval(leftchild(r)) b = eval(rightchild(r)) return a operator(r) b

Example: how to avoid recursion with a stack? in-order in binary trees: stack = empty; v = root 1: if (v.left!= null): stack.push(v) v = v.left goto 1 2: visit(v) if (v.right!= null): v = v.right goto 1 if (!stack.empty()) v = stack.pop() goto 2

: a General Scheme Systematic traverse through the whole graph: start visiting from a single node and moving along edges from already visited nodes, visit each node and edge available from s exactly once In each iteration: select next already visited node and visit all its outgoing, non-visited edges, and non-visited end-nodes In the above general scheme, by specifying the way of selecting the next visited node we obtain various renements of the algorithm

BFS and DFS - Important Variants of Two particularly important graph traversals: BFS (breadth-rst search) (next nodes to visit are kept on queue) DFS (depth-rst search) (next nodes to visit are kept on stack) Both produce resulting forest and (as a side product) classify each edge into one of four categories: tree (T) edge (edge of the resulting forest) forward (F) edge (in the same branch of the forest) backward (B) (as above but counter-directed) cross (C) (between two dierent branches or trees)

Breadth-rst search (BFS) graph G<V,E>; s - start node; d - distance from s, p - parent for-each node in V: node.color = white; u.d = infinity; u.p = null s.color = gray; s.d = 0; queue.in(s) while(!queue.empty()){ currnode = queue.out() process(currnode) for-each node in currnode.adjlist: if (node.color == white): queue.in(node) node.color = gray node.d = currnode.d + 1 node.p = currnode } currnode.color = black white - untouched; gray - waiting for processing; black - processed

Properties of BFS O(m + n) (dom. op.: set or update distance) the resulting tree (recorded in the parent array) species the shortest paths from s to other nodes

Depth-rst search (DFS) a Recursive Version d discovery time; f nishing time DFS(){ time = 0 for-each v in V: v.color = white; v.parent = null for-each v in V: if (v.color == white): recursivedfs(v) } recursivedfs(node v){ v.d = time++ v.color = gray process(v) for-each u in v.adjlist: if (u.color == white): u.parent = v recursivedfs(u) v.color = black v.f = time++ } white - before d is set; gray between d is set and f is set; black after f is set

Properties of DFS O(m + n) time complexity DFS can be obtained by modication of BFS - Queue should be replaced by Stack for any u, v V either the intervals (u.d, u.f ), (v.d, v.f ) are disjoint or one is completely included in the other (so called: parentheses structure) when DFS rst visits an edge (u, v): T if v white, B if v gray, F or C if v black undirected DFS: only T or B edges may happen (no C or F) DAG DFS: only T may happen (a good test for acyclicity)

Applications of DFS DFS has many important applications, for example: directed: topological sort directed: nding SCCs undirected: nding BCC (bi-connected components: maximum subsets of edges, so that any 2 edges in BCC lie on a common simple cycle; bridges or articulation points connect dierent BCCs; bridge: an edge which removed increases the number of SCCs; articulation point - a node with such property)

Topological Sort G - a DAG. Sort the nodes so that all the edges point from left to right application in scheduling: (V set of tasks, (u, v) task u must be done before v) Topological Sort: 1 compute nishing times v.f, v V with DFS on G 2 sort decreasingly by v.f Remark: If cycles are present ideal sorting impossible, but it minimises the backward edges

Computing SCCs Application of DFS to compute strongly connected components: 1 compute nishing times v.f, v V with DFS on G 2 reverse the arcs in G (transposed adjacency matrix) 3 run DFS on the reversed graph; apply decreasing order of v.f in the main loop of DFS 4 result: separate trees == separate SCCs

Questions/Problems: all basic graph denitions trees (denitions) graph computer representations (dierences/advantages, etc.) binary trees and visiting them: in-order, pre-order, post-order classication of edges in BFS and DFS BFS + properties DFS + properties compare DFS and BFS Topological Sort (high-level idea) (*) Other applications of DFS

Thank you for your attention