Algorithm Design and Analysis

Similar documents
Algorithm Design and Analysis

Algorithm Design and Analysis

CS4800: Algorithms & Data Jonathan Ullman

Algorithm Design and Analysis

12 Abstract Data Types

Algorithms: Lecture 10. Chalmers University of Technology

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

TIE Graph algorithms

Elementary Graph Algorithms

Copyright 2000, Kevin Wayne 1

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

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

CSI 604 Elementary Graph Algorithms

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

CSE 100: GRAPH ALGORITHMS

Graph: representation and traversal

Graph Algorithms: Chapters Part 1: Introductory graph concepts

TIE Graph algorithms

Announcements Problem Set 4 is out!

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

Course Review for Finals. Cpt S 223 Fall 2008

Data Structures Question Bank Multiple Choice

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Fundamental Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph

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

Review: Graph Theory and Representation

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

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

Representations of Graphs

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS 310 Advanced Data Structures and Algorithms

3.1 Basic Definitions and Applications

COT 6405 Introduction to Theory of Algorithms

Graph Algorithms. Definition

CSC263 Week 8. Larry Zhang.

Course Review. Cpt S 223 Fall 2009

Graph Algorithms Using Depth First Search

Topic 12: Elementary Graph Algorithms

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

Design and Analysis of Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

Introduction to Algorithms

3.1 Basic Definitions and Applications

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

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

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

Basic Data Structures (Version 7) Name:

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

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

Lecture Summary CSC 263H. August 5, 2016

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Week 12: Minimum Spanning trees and Shortest Paths

Chapter 9. Priority Queue

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

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

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

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

Introduction to Algorithms

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

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

Lecture 26: Graphs: Traversal (Part 1)

Graph Representations and Traversal

W4231: Analysis of Algorithms

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Trees Algorhyme by Radia Perlman

Minimum Spanning Trees Ch 23 Traversing graphs

Brute Force: Selection Sort

Chapter 22 Elementary Graph Algorithms

CS 206 Introduction to Computer Science II

csci 210: Data Structures Graph Traversals

CS301 - Data Structures Glossary By

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CS2 Algorithms and Data Structures Note 9

(Re)Introduction to Graphs and Some Algorithms

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Shortest Path Algorithms

Outlines: Graphs Part-2

Course Review. Cpt S 223 Fall 2010

Algorithm Design (8) Graph Algorithms 1/2

CMSC 341 Lecture 14: Priority Queues, Heaps

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

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

Data Structures and Algorithms CSE 465

Graph Representation

COMP 251 Winter 2017 Online quizzes with answers

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Announcements. HW3 is graded. Average is 81%

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

CS24 Week 8 Lecture 1

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Graphs. Terminology. Graphs & Breadth First Search (BFS) Extremely useful tool in modeling problems. Consist of: Vertices Edges

Stacks, Queues and Hierarchical Collections

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Transcription:

Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1

Measuring Running Time Focus on scalability: parameterize the running time by some measure of size (e.g. n = number of men and women) Kinds of analysis Worst-case Average-case (requires knowing the distribution) Best-case (how meaningful?) Exact times depend on computer; instead measure asymptotic growth L2.2

Computational Model Unless explicitly stated otherwise All numbers and pointers fit into a single word (block) of memory Constant-time operations Operations on words: arithmetic op s, shifts, comparisons, etc Following a pointer Array lookup Ignore cache, virtual memory, pretend everything fits in RAM We will sometimes drop these assumptions E.g.: for numerical problems, we might count bit operations L2.3

Data structures L1.4

Data Structures vs Abstract Data Types Data structure: concrete representation of data Array Linked list implemented with pointers Binary heap in array Adjacency list representation Abstract Data Type (ADT) : set of operations and their semantics (meaning/behavior) Priority queue Stack, queue Graph Dictionary L2.5

Basic Data Structures Lists O(1) time: Insert/delete anywhere we have a pointer Array O(1) time: append, lookup Good for Stack ADT: Last in, First out (LIFO) O(1) time: Push, pop Queue ADT: First in, First out (FIFO) O(1) time: enqueue, dequeue L2.6

Dictionary ADT Dictionary: Set of (key,value) pairs. Operations on dictionary S S.Insert(key, value) S.Find(key) S.delete(key) (Definitions of how to handle repeated keys vary.) L2.7

Dictionary Data Structures Data Struct. Find Insert Delete (after Find) Unsorted array ϴ(n) ϴ(1) ϴ(1) Linked list ϴ(n) ϴ(1) ϴ(1) Sorted array ϴ(log(n)) ϴ(n) ϴ(n) Binary search tree ϴ(height) ϴ(height) ϴ(height) Balanced binary search tree ϴ(log(n)) ϴ(log(n)) ϴ(log(n)) Hash table (expected time over the choice of hash function; worst case over data) 1 1 1 Here n = # of items currently in dictionary. Table entries are worst-case asymptotic running times. L2.8

Priority Queue ADT Set of (key, value) pairs Values are unique, keys are not Operations Q.Insert(k,v) Q.Changekey(v, k new ) Q.Extract-min() Often implemented as a binary heap KT Chapter 2.4 L2.9

Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a tape machine with two reels) What if you only have one stack and constant memory? Can you still simulate arbitrary access to an array? (Hint: think about pushdown automata.) L2.10

Graphs L3.11

Graphs (KT Chapter 3) Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of a set V of vertices (synonym: nodes), a set E V V of edges An edge e=(u,v) goes from u to v (may or may not allow u=v) In an undirected graph G = (V, E), the edge set E consists of unordered pairs of vertices Sometimes write e={u,v} How many edges can a graph have? In either case, E = O( V 2 ). L3.12

Graphs are everywhere L3.13

Graphs are everywhere Example Nodes Edges Transportation network: airline routes airports nonstop flights Communication networks computers, hubs, routers physical wires Information network: web pages hyperlinks Information network: scientific papers articles references Social networks people u is v s friend, u sends email to v, u s MySpace page links to v L3.14

Paths and Connectivity Path = sequence of consecutive edges in E (u,w 1 ), (w 1,w 2 ), (w 2,w 3 ),, (w k-1, v) Write u v or u v (Note: in a directed graph, direction matters) Undirected graph G is connected if for every two vertices u,v, there is a path from u to v in G L3.15

Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. G is connected. G does not contain a cycle. G has n-1 edges. L3.18

Rooted Trees Rooted tree: Given a tree T, choose a root node r and orient each edge away from r. Models hierarchical structure. root r parent of v v a tree the same tree, rooted at 1 child of v L3.19

Phylogeny Trees Phylogeny trees. Describe evolutionary history of species. L3.20

Parse Trees Internal representation used by compiler, e.g.: if (A[x]==2) then (32 2 + (a*64 +12)/8) else fibonacci(n) if-then-else == + fn-call array ref 2 power + / 8 fibonacci n A x 32 2 * 12 a 64 L3.21

Lecture Notes for Chapter 22: Elementary Graph Paths and Connectivity Example Directed graph? Strongly connected if for every pair, u v and v u directed graph [undirected example in bo s a b 0 1 3 1 c e 2 f g h 3 2 3 i 3 L3.22

Exploring a graph Classic problem: Given vertices s,t V, is there a path from s to t? Idea: explore all vertices reachable from s Two basic techniques: Breadth-first search (BFS) Explore children in order of distance to start node Depth-first search (DFS) How to convert these descriptions to precise algorithms? Recursively explore vertex s children before exploring siblings L3.23

Breadth First Search BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time. BFS algorithm. L 0 = { s }. L 1 = all neighbors of L 0. s L 1 L 2 L n-1 L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge to a node in L 1. L i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L i. L3.24

Breadth First Search L 0 L 1 L 2 L 3 L3.25

Breadth First Search Distance(u, v): number of edges on shortest path from u to v Properties. Let T be a BFS tree of G = (V, E). Nodes in layer i have distance i from root s Let (x, y) be an edge of G. Then the levels of x and y differ by at most 1. L 0 L 1 L 2 L 3 L3.26

Example BFS example (directed) directed graph [undirected example in b s a b 0 1 3 1 c e 2 f g h 3 2 3 i 3 Can show that Q consists of vertices wit L3.31

Implementing Traversals Generic traversal algorithm 1. R = {s} 2. While there is an edge (u,v) where u R and v R, Add v to R To implement this, need to choose Graph representation Data structures to track Vertices already explored Edge to be followed next These choices affect the order of traversal L3.32

Adjacency-matrix representation The adjacency matrix of a graph G = (V, E), where V = {1, 2,, n}, is the matrix A[1.. n, 1.. n] given by A[i, j] = A 1 2 3 4 1 if (i, j) E, 0 if (i, j) E. 2 1 1 2 0 1 1 0 0 0 1 0 Storage: ϴ(V 2 ) Good for dense graphs. 3 4 3 4 0 0 0 0 0 0 1 0 Lookup: O(1) time List all neighbors: O( V ) L3.33

Adjacency list representation An adjacency list of a vertex v V is the list Adj[v] of vertices adjacent to v. 2 1 3 4 Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} For undirected graphs, Adj[v] = degree(v). For digraphs, Adj[v] = out-degree(v). How many entries in lists? 2 E Total ϴ(V + E) storage. Typical notation: n = V = # vertices m = E = # edges Storage: ϴ(V+E) Good for sparse graphs. List all neighbors: O(degree) time Lookup(u,v): O(min(degree(u),degree(v)) time L3.34

Other representations? Can we get O(1) lookup /insertion/deletion O(degree(v)) list all neighbors of v O(V + E) storage? (Hint: hash tables) L3.35

BFS with adjacency lists d[1.. n]: array of integers initialized to infinity use to track distance from root (infinity = vertex not yet explored) Queue Q initialized to empty Tree T initialized to empty L3.36

BFS pseudocode BFS(s): 1. Set d[s]=0 2. Add s to Q 3. While (Q not empty) a) Dequeue (u) b) For each edge (u,v) adjacent to u a) If d[v] == then a) Set d[v] =d[u]+1 b) Add edge (u,v) to tree T c) Enqueue v onto Q O(1) time, run once overall. O(1) time, run once per vertex O(1) time per execution, run at most twice per edge Total: O(m+n) time (linear in input size) L3.37

Notes If s is the root of BFS tree, For every vertex u, path in BFS tree from s to u is a shortest path in G depth in BFS tree = distance from u to s Proof of BFS correctness: see KT, Chapter 3. L3.38

BFS Review Recall: Digraph G is strongly connected if for every pair of vertices, s t and t s Question: Give an algorithm for determining if a graph is strongly connected. What is the running time? L3.39