Upload
mercer
View
48
Download
0
Tags:
Embed Size (px)
DESCRIPTION
UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2004. Final Review. Review of Key Course Material. What’s It All About?. Algorithm : steps for the computer to follow to solve a problem Problem Solving Goals : recognize structure of some common problems - PowerPoint PPT Presentation
Citation preview
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Fall, 2004
UMass Lowell Computer Science 91.404
Analysis of Algorithms Prof. Karen Daniels
Fall, 2004
FinalFinal ReviewReview
Review of Key Course MaterialReview of Key Course Material
What’s It All About?What’s It All About?
AlgorithmAlgorithm:: steps for the computer to follow to steps for the computer to follow to
solve a problemsolve a problem Problem Solving GoalsProblem Solving Goals::
recognize structure of some common recognize structure of some common problemsproblems
understand important characteristics of understand important characteristics of algorithms to solve common problemsalgorithms to solve common problems
select appropriate algorithm & data select appropriate algorithm & data structures to solve a problemstructures to solve a problem
tailor existing algorithmstailor existing algorithms create new algorithmscreate new algorithms
Some Algorithm Application AreasSome Algorithm Application Areas
Computer GraphicsComputer Graphics
Geographic Geographic Information SystemsInformation Systems
RoboticsRobotics
BioinformaticsBioinformatics
AstrophysicsAstrophysicsMedical ImagingMedical Imaging
TelecommunicationsTelecommunications
Design
Apply
Analyze
Tools of the TradeTools of the Trade
Algorithm Design Patterns such as:Algorithm Design Patterns such as: binary searchbinary search divide-and-conquerdivide-and-conquer randomizedrandomized
Data Structures such as:Data Structures such as: trees, linked lists, stacks, queues, hash trees, linked lists, stacks, queues, hash
tables, graphs, heaps, arrays tables, graphs, heaps, arrays
Growth of FunctionsGrowth of Functions
SummationsSummations
RecurrencesRecurrences
SetsSets
ProbabilityProbability
MATHMATHProofsProofs
Discrete Math ReviewDiscrete Math Review
Growth of Functions, Summations, Growth of Functions, Summations, Recurrences, Sets, Counting, ProbabilityRecurrences, Sets, Counting, Probability
TopicsTopics
Discrete Math Review Discrete Math Review : : Sets, Basic Tree & Graph conceptsSets, Basic Tree & Graph concepts Counting: Permutations/CombinationsCounting: Permutations/Combinations Probability: Basics, including Expectation of a Random VariableProbability: Basics, including Expectation of a Random Variable Proof Techniques: InductionProof Techniques: Induction
Basic Algorithm Analysis TechniquesBasic Algorithm Analysis Techniques: : Asymptotic Growth of FunctionsAsymptotic Growth of Functions Types of Input: Best/Average/WorstTypes of Input: Best/Average/Worst Bounds on Algorithm vs. Bounds on ProblemBounds on Algorithm vs. Bounds on Problem Algorithmic Paradigms/Design Patterns: Algorithmic Paradigms/Design Patterns:
Divide-and-Conquer, RandomizedDivide-and-Conquer, Randomized Analyze pseudocode running time to form summations &/or Analyze pseudocode running time to form summations &/or
recurrencesrecurrences
What are we measuring?What are we measuring?
Some Analysis Criteria:Some Analysis Criteria: ScopeScope
The problem itself? The problem itself? A particular algorithm that solves the problem?A particular algorithm that solves the problem?
““Dimension”Dimension” Time Complexity? Space Complexity?Time Complexity? Space Complexity?
Type of BoundType of Bound Upper? Lower? Both?Upper? Lower? Both?
Type of InputType of Input Best-Case? Average-Case? Worst-Case?Best-Case? Average-Case? Worst-Case?
Type of ImplementationType of Implementation Choice of Data StructureChoice of Data Structure
Function Order of GrowthFunction Order of Growth
O( ) upper boundO( ) upper bound
( ) lower bound( ) lower bound
( ) upper & lower bound( ) upper & lower bound
nn11 n lg(n) n lg(n) n lgn lg22(n) (n) 22nnnn55 lg(n)lg(n)lglg(n) lglg(n) nn22
know how to use asymptotic complexity notationknow how to use asymptotic complexity notation
to describe time or space complexityto describe time or space complexity
know how to order functions know how to order functions asymptoticallyasymptotically
(behavior as n becomes large)(behavior as n becomes large)
shorthand for shorthand for inequalitiesinequalities
Types of Algorithmic InputTypes of Algorithmic Input
Best-Case InputBest-Case Input: of all possible algorithm inputs of size n, it : of all possible algorithm inputs of size n, it generates the “best” resultgenerates the “best” result for Time Complexity: “best” is smallest running timefor Time Complexity: “best” is smallest running time
Best-Case Input Produces Best-Case Running TimeBest-Case Input Produces Best-Case Running Time provides a provides a lower bound on the algorithm’s asymptotic running timelower bound on the algorithm’s asymptotic running time (subject to any implementation assumptions)(subject to any implementation assumptions)
for Space Complexity: “best” is smallest storagefor Space Complexity: “best” is smallest storage
Average-Case InputAverage-Case InputWorst-Case InputWorst-Case Input
these are defined similarlythese are defined similarly
Best-Case Time <= Average-Case Time <= Worst-Case TimeBest-Case Time <= Average-Case Time <= Worst-Case Time
Bounding Algorithmic Time(using cases)
Bounding Algorithmic Time(using cases)
nn11 n lg(n) n lg(n) n lgn lg22(n) (n) 22nnnn55 lg(n)lg(n)lglg(n) lglg(n) nn22
T(n) = T(n) = (1)(1) T(n) = T(n) = (2(2nn))very loose bounds are not very useful!very loose bounds are not very useful!
Worst-CaseWorst-Case time of T(n) = time of T(n) = (2(2nn) tells us that worst-case inputs cause the algorithm to ) tells us that worst-case inputs cause the algorithm to take take at mostat most exponential time (i.e. exponential time is exponential time (i.e. exponential time is sufficientsufficient). ).
But, can the algorithm every really take exponential time? (i.e. is exponential timeBut, can the algorithm every really take exponential time? (i.e. is exponential time necessarynecessary?)?)
If, If, for arbitrary nfor arbitrary n, we find a worst-case input that forces the algorithm to use exponential , we find a worst-case input that forces the algorithm to use exponential time, then this time, then this tightenstightens the the lower boundlower bound on the on the worst-caseworst-case running time. If we can force running time. If we can force the lower and upper bounds on the worst-case time to match, then we can say that, for the lower and upper bounds on the worst-case time to match, then we can say that, for the worst-case running time, T(n) = the worst-case running time, T(n) = (2(2n n ) (i.e. we’ve found the ) (i.e. we’ve found the minimum upper boundminimum upper bound, , so the bound is so the bound is tight.) tight.)
Using “case” we can discuss lower and/or upper bounds on:Using “case” we can discuss lower and/or upper bounds on:best-case running timebest-case running time or or average-case running timeaverage-case running time or or worst-case running timeworst-case running time
Bounding Algorithmic Time(tightening bounds)
Bounding Algorithmic Time(tightening bounds)
nn11 n lg(n) n lg(n) n lgn lg22(n) (n) 22nnnn55 lg(n)lg(n)lglg(n) lglg(n) nn22
TTWW (n) = (n) = (2(2nn))
for example...for example...
Here we denote best-case time by THere we denote best-case time by TBB(n); worst-case time by T(n); worst-case time by TWW(n) (n)
TTBB(n) = (n) = (1)(1)1st attempt1st attempt 1st attempt1st attempt
TTBB (n) = (n) = (n)(n)
1st attempt1st attempt
2nd attempt2nd attempt
TTWW (n) = (n) = (n(n22))
TTBB(n) = (n) = (n)(n)2nd attempt2nd attempt
1st attempt1st attempt
TTWW(n) = (n) = (n(n22))
Algorithm BoundsAlgorithm Bounds
Explore the Explore the problem problem to gain intuition:to gain intuition: Describe it: What are the assumptions? Describe it: What are the assumptions? (model of computation, (model of computation,
etc...)etc...) Has it already been solved?Has it already been solved? Have similar problems been solved? (more on this later)Have similar problems been solved? (more on this later) What does best-case input look like?What does best-case input look like? What does worst-case input look like?What does worst-case input look like?
Establish worst-case Establish worst-case upper boundupper bound on the on the problemproblem using using an algorithman algorithm Design a (simple) algorithm and find an upper bound on its worst-case Design a (simple) algorithm and find an upper bound on its worst-case
asymptotic running time; this tells us problem asymptotic running time; this tells us problem can becan be solved in a certain solved in a certain amount of time. Algorithms taking more than this amount of time may amount of time. Algorithms taking more than this amount of time may exist, but won’t help us.exist, but won’t help us.
Establish worst-case Establish worst-case lower boundlower bound on the on the problemproblem Tighten each bound to form a worst-case “sandwich” Tighten each bound to form a worst-case “sandwich”
Approach
increasing worst-case asymptotic running time as a function of nincreasing worst-case asymptotic running time as a function of n
nn11 22nnnn22 nn33 nn44 nn55
Know the Difference!Know the Difference!
nn11 22nnnn55
worst-case boundsworst-case bounds on problemon problem
An inefficient algorithm for An inefficient algorithm for the problem might exist the problem might exist that takes this much time, that takes this much time, but would not help us.but would not help us.
No algorithm for the problem No algorithm for the problem exists that can solve it for exists that can solve it for worst-case inputs in less worst-case inputs in less than linear time .than linear time .
Strong BoundStrong Bound: This : This worst-case worst-case lowerlower bound bound on the problem holds for on the problem holds for everyevery algorithm that algorithm that solves the problem and solves the problem and abides by our problem’s abides by our problem’s assumptions.assumptions.
Weak BoundWeak Bound: This worst-case : This worst-case upperupper bound on the problem bound on the problem comes from just considering comes from just considering oneone algorithm. Other, less algorithm. Other, less efficient algorithms that solve efficient algorithms that solve this problem might exist, but this problem might exist, but we don’t care about them!we don’t care about them!
Both the upper and lower bounds Both the upper and lower bounds are probably are probably looseloose (i.e. probably (i.e. probably can be can be tightenedtightened later on). later on).
Master TheoremMaster TheoremMMaster Theorem : LLet with a > 1 and b > 1 .Tthen : CCase 1: If f(n) = O ( n (log
b a) - ) for some > o
T then T ( n ) = ( n log b
a ) CCase 2: If f (n) = (n log
b a )
T then T ( n ) = (n log b
a * log n ) CCase 3: If f ( n ) = (n (log
ba) + ) for some > o and if
a f( n/b) < c f ( n ) for some c < 1 , n > N0 T then T ( n ) = ( f ( n ) )
)()()( nfb
naTnT
Use ratio test to Use ratio test to distinguish distinguish between cases:between cases:
f(n)/ f(n)/ n log b
a
Look for Look for “polynomially “polynomially larger” dominance.larger” dominance.
Master TheoremMaster Theorem
CS Theory Math Review SheetThe Most Relevant Parts...CS Theory Math Review SheetThe Most Relevant Parts...
p. 1p. 1 O, O, , , definitions definitions Series Series CombinationsCombinations
p. 2 Recurrences & p. 2 Recurrences & Master MethodMaster Method
p. 3p. 3 ProbabilityProbability FactorialFactorial LogsLogs Stirling’s approxStirling’s approx
p. 4 Matricesp. 4 Matrices p. 5 Graph Theoryp. 5 Graph Theory p. 6 Calculusp. 6 Calculus
Product, Quotient Product, Quotient rulesrules
Integration, Integration, DifferentiationDifferentiation
Logs Logs p. 8 Finite Calculusp. 8 Finite Calculus p. 9 Seriesp. 9 Series
Math fact sheet (courtesy of Prof. Costello) is on our web site.Math fact sheet (courtesy of Prof. Costello) is on our web site.
SortingChapters 6-9
SortingChapters 6-9
Heapsort, Quicksort, LinearTime-SortingHeapsort, Quicksort, LinearTime-Sorting
TopicsTopics
SortingSorting: : Chapters 6-8Chapters 6-8 Sorting Algorithms:Sorting Algorithms:
[Insertion & MergeSort)], Heapsort, Quicksort, LinearTime-Sorting[Insertion & MergeSort)], Heapsort, Quicksort, LinearTime-Sorting Comparison-Based Sorting and its lower boundComparison-Based Sorting and its lower bound Breaking the lower bound using special assumptionsBreaking the lower bound using special assumptions Tradeoffs: Selecting an appropriate sort for a given situationTradeoffs: Selecting an appropriate sort for a given situation
Time vs. Space RequirementsTime vs. Space Requirements Comparison-Based vs. Non-Comparison-BasedComparison-Based vs. Non-Comparison-Based
Heaps & HeapSortHeaps & HeapSort
Structure:Structure: Nearly complete binary treeNearly complete binary tree Convenient array representationConvenient array representation
HEAP Property: HEAP Property: (for MAX HEAP)(for MAX HEAP) Parent’s label not less than that of each childParent’s label not less than that of each child
Operations: Operations: strategy worst-case run-timestrategy worst-case run-time HEAPIFY: HEAPIFY: swap downswap down O(h) [h= ht]O(h) [h= ht] INSERT: INSERT: swap upswap up O(h)O(h) EXTRACT-MAX: EXTRACT-MAX: swap, swap, HEAPIFYHEAPIFY O(h)O(h) MAX: MAX: view rootview root O(1)O(1) BUILD-HEAP: BUILD-HEAP: HEAPIFYHEAPIFY O(n)O(n) HEAP-SORT: HEAP-SORT: BUILD-HEAP, HEAPIFYBUILD-HEAP, HEAPIFY (nlgn)(nlgn)
1616
1414 1010
88 77 99 33
22 44 11
1616 1414 1010 88 77 99 33 22 44 111 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
QuickSortQuickSort
Divide-and-Conquer StrategyDivide-and-Conquer Strategy Divide: Partition arrayDivide: Partition array Conquer: Sort recursivelyConquer: Sort recursively Combine: No work neededCombine: No work needed
Asymptotic Running Time:Asymptotic Running Time: Worst-Case: Worst-Case: (n(n22) ) (partitions of size 1, n-1)(partitions of size 1, n-1)
Best-Case: Best-Case: (nlgn)(nlgn) (balanced partitions of size n/2)(balanced partitions of size n/2)
Average-Case: Average-Case: (nlgn) (nlgn) (balanced partitions of size n/2)(balanced partitions of size n/2) Randomized PARTITION Randomized PARTITION
selects partition element randomlyselects partition element randomly imposes uniform distributionimposes uniform distribution
Does most of the work on the way down Does most of the work on the way down (unlike MergeSort, which does most of (unlike MergeSort, which does most of work on the way back up (in Merge).work on the way back up (in Merge).
)())()((max)( 11 nqnTqTnT nq
99 77 33 22 44 11 1616 1414 1010 1111
9 9
)())()((min)( 11 nqnTqTnT nq
)())()(()( nqnTqTlueExpectedVanT
PARTITIONPARTITION
Recursively sort right partitionRecursively sort right partition
right partitionright partitionleft partitionleft partition
Recursively sort left partitionRecursively sort left partition
Comparison-Based SortingComparison-Based Sorting
In algebraic decision tree model, comparison-based In algebraic decision tree model, comparison-based sorting of n items requiressorting of n items requires (n lg n) (n lg n) worst-case time.worst-case time.
HeapSortHeapSort
To break the lower bound and obtain linear To break the lower bound and obtain linear time, forego direct value comparisons and/or time, forego direct value comparisons and/or
make stronger assumptions about input. make stronger assumptions about input.
InsertionSortInsertionSort
MergeSortMergeSort
QuickSortQuickSort
(n) (n) (n(n22) )
BestCaseBestCase AverageCaseAverageCase WorstCaseWorstCaseTimeTime::AlgorithmAlgorithm::
(n lg n) (n lg n) (n (n lg n) lg n)
(n lg n)* (n lg n)* (n lg n) (n lg n)
(n lg n)(n lg n) (n lg n) (n lg n) (n(n22) )
(*when all elements are distinct)(*when all elements are distinct)
Data StructuresChapters 10-13Data StructuresChapters 10-13
Stacks, Queues, LinkedLists, Trees, Stacks, Queues, LinkedLists, Trees, HashTables, Binary Search Trees, Balanced HashTables, Binary Search Trees, Balanced
TreesTrees
TopicsTopics
Data StructuresData Structures: : Chapters 10-13Chapters 10-13 Abstract Data Types: their properties/invariantsAbstract Data Types: their properties/invariants
Stacks, Queues, LinkedLists, (Heaps from Chapter 6), Trees, Stacks, Queues, LinkedLists, (Heaps from Chapter 6), Trees, HashTables, Binary Search Trees, Balanced (Red/Black) TreesHashTables, Binary Search Trees, Balanced (Red/Black) Trees
Implementation/Representation choices -> data structureImplementation/Representation choices -> data structure Dynamic Set Operations:Dynamic Set Operations:
Query [does not change the data structure]Query [does not change the data structure] Search, Minimum, Maximum, Predecessor, SuccessorSearch, Minimum, Maximum, Predecessor, Successor
Manipulate: [can change data structure]Manipulate: [can change data structure] Insert, DeleteInsert, Delete
Running Time & Space Requirements for Dynamic Set Running Time & Space Requirements for Dynamic Set Operations for each Data Structure Operations for each Data Structure
Tradeoffs: Selecting an appropriate data structure for a situationTradeoffs: Selecting an appropriate data structure for a situation Time vs. Space RequirementsTime vs. Space Requirements Representation choicesRepresentation choices Which operations are crucial?Which operations are crucial?
Hash TableHash Table
Structure:Structure: n << N n << N (number of keys in table much smaller than size of key universe)(number of keys in table much smaller than size of key universe)
Table with m elementsTable with m elements m typically primem typically prime
Hash Function: Hash Function: Not necessarily a 1-1 mapping Not necessarily a 1-1 mapping Uses mod m to keep index in tableUses mod m to keep index in table
Collision Resolution:Collision Resolution: Chaining: linked list for each table entryChaining: linked list for each table entry Open addressing: all elements in tableOpen addressing: all elements in table
Linear Probing:Linear Probing:
Quadratic Probing:Quadratic Probing:
mkkh mod)(
mn /Load Factor:Load Factor:
Example:Example:
mikhikh mod))('(),(
micickhikh mod))('(),( 221
Linked ListsLinked Lists
TypesTypes SinglySingly vs. Doubly linked vs. Doubly linked
Pointer to Pointer to HeadHead andand/or /or TailTail
NonCircular vs. NonCircular vs. CircularCircular
Type influences running time of operationsType influences running time of operations
99 44 33 //headhead
99 44 33 //headhead
tailtail
99 44 33 headhead
Binary Tree TraversalBinary Tree Traversal
““Visit” each node onceVisit” each node once Running time in Running time in (n) for an n-node binary tree(n) for an n-node binary tree PreorderPreorder: : ABDCEFABDCEF
Visit nodeVisit node Visit left subtreeVisit left subtree Visit right subtreeVisit right subtree
InorderInorder: : DBAEFCDBAEFC Visit left subtreeVisit left subtree Visit nodeVisit node Visit right subtreeVisit right subtree
PostorderPostorder: : DBFECADBFECA Visit left subtreeVisit left subtree Visit right subtreeVisit right subtree Visit nodeVisit node
B
E
C
F
D
A
Binary Search TreeBinary Search Tree
B
D
F
E
A
C Structure:Structure: Binary treeBinary tree
BINARY SEARCH TREE Property: BINARY SEARCH TREE Property: For each pair of nodes u, v:For each pair of nodes u, v:
If u is in left subtree of v, then key[u] <= key[v]If u is in left subtree of v, then key[u] <= key[v] If u is in right subtree of v, then key[u] >= key[v]If u is in right subtree of v, then key[u] >= key[v]
Operations: Operations: strategy strategy worst-case run-timeworst-case run-time TRAVERSAL: TRAVERSAL: INORDER, PREORDER, POSTORDERINORDER, PREORDER, POSTORDER O(h) [h= ht]O(h) [h= ht] SEARCH: SEARCH: traverse 1 branch using BST propertytraverse 1 branch using BST property O(h)O(h) INSERT: INSERT: searchsearch O(h)O(h) DELETE: DELETE: splice out (cases depend on # children)splice out (cases depend on # children) O(h)O(h) MIN: MIN: go leftgo left O(h)O(h) MAX: MAX: go rightgo right O(h)O(h) SUCCESSOR: MIN SUCCESSOR: MIN if rt subtree; else go upif rt subtree; else go up O(h)O(h) PREDECESSOR: PREDECESSOR: analogous to SUCCESSORanalogous to SUCCESSOR O(h)O(h)
Navigation RulesNavigation Rules Left/Right Rotations that preserve BST propertyLeft/Right Rotations that preserve BST property
Red-Black Tree Properties
Every node in a red-black tree is either black or redEvery node in a red-black tree is either black or red Every null leaf is blackEvery null leaf is black No path from a leaf to a root can have two consecutive red nodes -- No path from a leaf to a root can have two consecutive red nodes --
i.e. the children of a red node must be blacki.e. the children of a red node must be black Every path from a node, Every path from a node, x,x, to a descendant leaf contains the same to a descendant leaf contains the same
number of black nodes -- the “black height” of node number of black nodes -- the “black height” of node xx..
newly inserted
node
Graph AlgorithmsChapter 22
Graph AlgorithmsChapter 22
DFS/BFS Traversals, Topological Sort DFS/BFS Traversals, Topological Sort
TopicsTopics
Graph AlgorithmsGraph Algorithms: : Chapter 22Chapter 22 Undirected, Directed GraphsUndirected, Directed Graphs Connected Components of an Undirected GraphConnected Components of an Undirected Graph Representations: Adjacency Matrix, Adjacency ListRepresentations: Adjacency Matrix, Adjacency List Traversals: DFS and BFSTraversals: DFS and BFS
Differences in approach: DFS: LIFO/stack vs. BFS:FIFO/queueDifferences in approach: DFS: LIFO/stack vs. BFS:FIFO/queue Forest of spanning treesForest of spanning trees Vertex coloring, Edge classification: tree, back, forward, crossVertex coloring, Edge classification: tree, back, forward, cross Shortest paths (BFS)Shortest paths (BFS)
Topological SortTopological Sort Tradeoffs:Tradeoffs:
Representation Choice: Adjacency Matrix vs. Adjacency ListRepresentation Choice: Adjacency Matrix vs. Adjacency List Traversal Choice: DFS or BFSTraversal Choice: DFS or BFS
Introductory Graph Concepts:RepresentationsIntroductory Graph Concepts:Representations
B
E
C
FD
A B
E
C
FD
A
Undirected GraphUndirected Graph Directed Graph (digraph)Directed Graph (digraph)
0 1 1 0 0 0
0 0 1 0 1 1
0 0 0 0 0 0
0 0 0 1 0 0
0 1 0 1 0 0
0 0 0 0 1 0
A B C D E FA B C D E FA A B B C C D D E E FF
0 1 1 0 0 0
1 0 1 0 1 1
1 1 0 0 0 0
0 0 0 0 1 0
0 1 0 1 0 1
0 1 0 0 1 0
A B C D E FA B C D E FA A B B C C D D E E FF
A BC A BC B ACEFB ACEFC ABC ABD ED EE BDFE BDFF BEF BE
A BC A BC B CEFB CEFC C D DD DE BDE BDF EF E
Adjacency Adjacency MatrixMatrix
Adjacency ListAdjacency List Adjacency Adjacency MatrixMatrix
Adjacency ListAdjacency List
Elementary Graph Algorithms:SEARCHINGSEARCHING: DFS, BFSElementary Graph Algorithms:SEARCHINGSEARCHING: DFS, BFS
Breadth-First-Search (BFS):Breadth-First-Search (BFS): BFS BFS vertices close to v are visited before vertices close to v are visited before
those further away those further away FIFO structure FIFO structure queue queue data structuredata structure
Shortest Path DistanceShortest Path Distance From From sourcesource to each reachable vertex to each reachable vertex Record during traversalRecord during traversal Foundation of many “Foundation of many “shortest pathshortest path” ”
algorithmsalgorithms
See DFS, BFS Handout for PseudoCodeSee DFS, BFS Handout for PseudoCode
Depth-First-Search (DFS):Depth-First-Search (DFS): DFS backtracks DFS backtracks visit most recently discovered vertex visit most recently discovered vertex
LIFO structure LIFO structure stack data structure stack data structure
Encountering, finishing timesEncountering, finishing times: : “well-formed” nested (( )( ) ) “well-formed” nested (( )( ) ) structurestructure
DFS of undirected graph produces only back edges or tree edgesDFS of undirected graph produces only back edges or tree edges Directed graph is acyclic if and only if DFS yields no back Directed graph is acyclic if and only if DFS yields no back
edges edges
for for unweighted unweighted directeddirected or or undirectedundirected graph G=(V,E) graph G=(V,E)TimeTime: O(|V| + |E|) adj list: O(|V| + |E|) adj list O(|V|O(|V|22) adj matrix) adj matrix
predecessor subgraph = forest of spanning treespredecessor subgraph = forest of spanning trees
Vertex color shows status:Vertex color shows status:not yet encounterednot yet encountered
encountered, but not yet finishedencountered, but not yet finished
finishedfinished
Elementary Graph Algorithms:DFS, BFSElementary Graph Algorithms:DFS, BFS
Review problem: TRUE or FALSE?Review problem: TRUE or FALSE? The tree shown below on the right can be a DFS tree for some The tree shown below on the right can be a DFS tree for some
adjacency list representation of the graph shown below on the adjacency list representation of the graph shown below on the
left.left. B
E
C
F
D
A
A
C
B
E
D
F
Tree Edge
Tree Edge
Tree Edge
Tree Edge
Tree Edge
Cross Edge
Back Edge
Elementary Graph Algorithms:Topological SortElementary Graph Algorithms:Topological Sort
source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.
TOPOLOGICAL-SORT(G)TOPOLOGICAL-SORT(G)1 DFS(G) computes “finishing times” for each vertex1 DFS(G) computes “finishing times” for each vertex2 as each vertex is finished, insert it onto front of list2 as each vertex is finished, insert it onto front of list3 return list3 return list
for Directed, Acyclic Graph (DAG) G=(V,E)
Produces linear ordering of vertices.Produces linear ordering of vertices.For edge (u,v), u is ordered before v.For edge (u,v), u is ordered before v.
See also 91.404 DFS/BFS slide showSee also 91.404 DFS/BFS slide show