CS61BL. Lecture 5: Graphs Sorting
|
|
- Eustace Taylor
- 5 years ago
- Views:
Transcription
1 CS61BL Lecture 5: Graphs Sorting
2 Graphs
3 Graphs Edge Vertex
4 Graphs (Undirected)
5 Graphs (Directed)
6 Graphs (Multigraph)
7 Graphs (Acyclic)
8 Graphs (Cyclic)
9 Graphs (Connected)
10 Graphs (Disconnected)
11 Graphs (Unweighted)
12 Graphs (Weighted)
13 Graph Representation Adjacency Matrix A Matrix (e.g. 2D array) specifying which edges are present in a graph A B C D E F A B D B F C D C E E F
14 Graph Representation Adjacency Matrix A 3 Matrix (e.g. 2D array) specifying which edges are present in a graph A B C D E F 1 A X 3 X X X X B D 1 B X X X 1 1 X -1 C 5 X X X X X F D X X X X X 1 C E 2 E X X 3 X X 2 3 F X X X X X X 5
15 Graph Representation Adjacency List Collection of lists of neighbors Each list specifies a single vertex s neighbors A B C D E F A: [B] B: [D, E] C: [A] D: [F] E: [C, F] F: [ ]
16 Graph Representation Adjacency List Collection of lists of neighbors Each list specifies a single vertex s neighbors A 3 5 B C D E 1 2 F A: [(B, 3)] B: [(D, 1), (E, -1)] C: [(A, 5)] D: [(F, 1)] E: [(C, 3), (F, 2)] F: [ ]
17 Graph Traversal Almost same as tree traversal Keep track of which vertices have been already been visited Pick any vertex to start at instead of root BFS, DFS
18 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E
19 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E A
20 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E
21 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E B
22 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E
23 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E D E
24 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E E
25 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E E F
26 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E F
27 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E F C
28 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E C
29 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E
30 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E
31 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E F
32 Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E
33 Shortest Path Problem Find shortest path from some start vertex s to some end vertex t Google Maps, puzzles, pathing AIs, etc Unweighted graph: use BFS Weighted graph:?
34 Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B 1 B 1 C A 3 A C
35 Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B 100? 1 C A 300
36 Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B ? 1 C A
37 Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B ? 1 C A
38 Dijkstra s Algorithm Finds shortest path from start vertex to every other vertex in graph Use priority queue as fringe Priority value of vertex u is shortest path length we ve found from starting vertex s so far
39 Dijkstra s Algorithm Example Add starting vertex s to priority queue w/ priority 0 Add all other vertices with priority While priority queue is not empty: u = vertex dequeued from priority queue For each neighbor v: If v is in priority queue, update v s priority value with minimum of: Current priority Priority of u + weight of edge (u, v)
40 Dijkstra s Algorithm Example Add s to priority queue with priority 0 Add all other vertices with priority Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E?????
41 Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeue d from priority queue Priority Queue A B C D E 1 B A 5 2 D 1 9 C 3 4 E Shortest Distances A B C D E 0????
42 Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 D 1 9 C 3 4 E Shortest Distances A B C D E 0????
43 Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0????
44 Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1???
45 Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1???
46 Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1???
47 Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3?
48 Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3?
49 Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3?
50 Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3 4
51 Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3 4
52 Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E B 2 D 1 1 A 5 9 C 3 4 E Shortest Distances A B C D E
53 Dijkstra Example Priority Queue A B C B A 2 C Shortest Distances A B C???
54 Dijkstra Example Priority Queue A B C B -2 3 A 2 C Shortest Distances A B C 0??
55 Dijkstra Example Priority Queue A B C B A 2 C Shortest Distances A B C 0??
56 Dijkstra Example Priority Queue A B C B A 2 C Shortest Distances A B C 0? 2
57 Dijkstra Example Priority Queue A B C B -2 3 A 2 C Shortest Distances A B C 0 3 2
58 Dijkstra Example Priority Queue A B C B -2 3 A 2 C Shortest Distances A B C 0 3 2
59 Another Example Priority Queue A B C B A 2 C Shortest Distances A B C???
60 Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C 0??
61 Another Example Priority Queue A B C B A 2 C Shortest Distances A B C 0??
62 Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C 0 1?
63 Another Example Priority Queue A B C B A 2 C Shortest Distances A B C 0 1?
64 Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C 0 1 3
65 Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C Cycles are fine
66 Why do we care about graphs? Facebook, LinkedIn Maps (Google Maps, GPS) The internet Puzzle configurations (Rubik s cube, project 3)
67 Final Exam Information Next Wednesday (8/13), 3-6pm Last name A-H, Q-Z: 2050 VLSB Last name J-P: 100 GPB All lab and quiz material up to and including this Friday s (balanced search trees) Review Session Sunday (8/10), 1-3pm in 100 GPB
68 Sorting Given a collection of elements, sort them from smallest to largest
69 Terminology: Stable Elements with the same value are ordered the same before and after sorting Original: Sorted (stable): Sorted (unstable):
70 Terminology: In-place Only uses O(1) additional space E.g. doesn t copy all contents of input array into a new array Used to describe any algorithm, not just sorting algorithms
71 Selection Sort for i = 0, 1,..., (n-1): Search positions i to(n-1)for smallest element Swap with element at position i Stable, in-place Best / average / worst case running time: O(n 2 ) for a list with n elements
72 Selection Sort Example Current index Smallest found this iteration Already sorted
73 Bubble Sort for i = 0, 1,..., (n-1): Go through the first n-i elements Swap pairs of adjacent elements that are out of order Stable, in-place Average / worst case running time: O n 2 for a list with n elements Best case running time: O n with optimization
74 Bubble Sort Example
75 Insertion Sort for i = 0, 1,..., (n-1): Take element at index i Insert it into the correct location in the sorted sublist consisting of the first i+1 elements Stable, in-place Average / worst case running time: O n 2 list with n elements Best case running time: O n for a
76 Insertion Sort Example
77 Insertion Sort Example
78 Insertion Sort Example
79 Insertion Sort Example
80 Insertion Sort Example
81 Insertion Sort Example
82 Insertion Sort Example
83 Insertion Sort Example
84 Insertion Sort Example
85 Insertion Sort Example
86 Insertion Sort Example
87 Insertion Sort Example
88 Insertion Sort Example
89 Insertion Sort Example
90 Insertion Sort Example
91 Insertion Sort Example
92 Insertion Sort Example
93 Insertion Sort Example
94 Insertion Sort Example
95 Insertion Sort Example
96 Insertion Sort Example
97 Insertion Sort Example
98 Insertion Sort Example
99 Insertion Sort Example
100 Insertion Sort Example
101 Insertion Sort Example
102 Insertion Sort Example
103 Insertion Sort Example
104 Insertion Sort Example
105 Insertion Sort Example
106 Insertion Sort Example
107 Insertion Sort Example
108 Insertion Sort Example
109 Insertion Sort Example
110 Insertion Sort Example
111 Insertion Sort Example
112 Insertion Sort Example
113 Insertion Sort Example
114 Insertion Sort Example
115 Insertion Sort Example
116 Insertion Sort Example
117 Insertion Sort Example
118 Insertion Sort Example
119 Insertion Sort Example
120 Faster Sorting Divide and Conquer: Solve large problem by splitting it into smaller instances of same problem
121 Heap Sort (From last lecture) Create heap Remove smallest element from heap, one at a time NOT Stable In-place* Best / average / worst case running time: O(n log n) for a list with n elements
122 In-place Heap Sort X
123 In-place Heap Sort X
124 In-place Heap Sort X
125 In-place Heap Sort X
126 In-place Heap Sort X
127 In-place Heap Sort Etc
128 In-place Heap Sort X
129 Merge Sort Base case: list of 1 element is already sorted Recursively sort left and right halves Merge Stable* Can be done in-place (not efficient) Best / average / worst case running time: O(n log n) for a list with n elements
130 Merge Sort Running Time
131 Merge Sort Running Time
132 Merge Sort Running Time 1 merge: lists of n 2 elements each 2 merges: lists of n 4 elements each n 2 merges: lists of 1 element each
133 Merge Sort Running Time 1 merge: lists of n elements each 2 2 merges: lists of n elements each 4 n 2 merges: lists of 1 element each O(n) O(n) O(n) log n depth
134 Merge Sort Running Time 1 merge: lists of n elements each 2 2 merges: lists of n elements each 4 n 2 merges: lists of 1 element each O(n) O(n) O(n) log n depth O(n log n)
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 Quick Sort Base case: list of 1 element is already sorted Choose one of the list elements as the pivot Partition list into elements that are less than pivot and elements that are greater than pivot Recursively do this on both partitions Stable, in-place Best / average case running time: O(n log n) where n is number of elements in input list Worst case running time: O(n 2 )
156 Quick Sort Running Time Pivot Pivot Pivot
157 Quick Sort Running Time Pivot Pivot
158 Quick Sort Running Time All partitioning at each depth level: O(n) time Number of depth levels: O(log n) to O(n) Total running time: O(n log n) to O(n 2 )
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 Sorting in practice Base case: use insertion sort on smaller lists Quick sort is fastest on randomized lists Choosing a pivot (options): Choose first element of list Calculate median Pick 3 elements at random and take median
176 Java s Arrays.sort Dual-pivot quicksort! Vladimir Yaroslavskiy, 2009 Used in Java s Arrays.sort starting with Java 7 (2011)
177 Comparison Sorts Compares elements Given items a and b: a > b? a < b? (a == b?) All sorting algorithms we ve learned so far Selection, bubble, insertion, merge, heap, quick
178 Comparison Sorts: Number of Comparisons (Lower Bound) Each comparison distinguishes between 2 possibilities f(n) comparisons 2 f n list permutations n elements: n! possible list permutations 2 f n n! f n log 2 (n!)
179 Comparison Sorts Lower bound on number of comparisons: log 2 (n!) Want to show: log 2 (n!) Ω(n log 2 n)
180 Want to show: log 2 (n!) Ω(n log 2 n) log 2 (n!) = log 2 n + log 2 n log 2 2 n > log 2 n + log 2 n log 2 2 > log 2 n 2 + log 2 n log 2 n 2 = n 2 log 2 n 2
181 Want to show: log 2 (n!) Ω(n log 2 n) log 2 n! > n log n Θ(n log 2 ) 2 Θ(n log 2 n n log 2 2) Θ(n log 2 n) log 2 (n!) Ω(n log 2 n) n Lower bound on number of comparisons: Ω(n log 2 n)
182 Comparison Sorts Lower bound on number of comparisons: Ω(n log 2 n)
183 Non-comparison Sorts We can do better
184 Counting Sort Input: Large unsorted list of n integers in the range [0, k] (hopefully k n) Solution: Create an int array of size k Traverse input list Increment int at array position i whenever you come across i
185 Counting Sort Example Input list Counter array
186 Counting Sort Example Input list Counter array
187 Counting Sort Example Input list Counter array
188 Counting Sort Example Input list Counter array
189 Counting Sort Example Input list Counter array
190 Counting Sort Example Input list Counter array
191 Counting Sort Example Input list Counter array
192 Counting Sort Example Input list Counter array
193 Counting Sort Example Output: Counter array
194 Counting Sort Example Output: Counter array
195 Counting Sort Example Output: 1 1 Counter array
196 Counting Sort Example Output: 1 1 Counter array
197 Counting Sort Example Output: Counter array
198 Counting Sort Example Output: Counter array
199 Counting Sort Example Output: Counter array
200 Counting Sort Example Output: Counter array
201 Counting Sort Example Output: Counter array
202 Counting Sort Best / average / worst case running time: O n + k Stable? N/A In-place? No
203 Radix Sort (version 1) Base case: list of only 1 element is already sorted Put elements into buckets according to most significant digit Recursively sort each bucket on next most significant digit (and so forth)
204 Radix Sort v1 Example Input list: 49, 26, 32, 39, 65, 61, 33, 31
205 Radix Sort v1 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Put elements into buckets according to most significant digit
206 Radix Sort v1 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Recursively sort on next most significant digit(s)
207 Radix Sort v1 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Output sorted list: 21, 26, 32, 33, 39, 49, 61,
208 Radix Sort (version 2) Base case: list of size 1 is already sorted Put elements into buckets according to least significant digit Output all of the elements to a list: In bucket order In the order they were added to each bucket Repeat with next digit until you reach the most significant digit
209 Radix Sort v2 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21
210 Radix Sort v2 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Put elements into buckets according to least significant digit
211 Radix Sort v2 Example Output all of the elements to a list: In bucket order In the order they were added to each bucket Output: 61, 21, 32, 33, 65, 26, 49,
212 Radix Sort v2 Example Input: 61, 21, 32, 33, 65, 26, 49, 39 Repeat with next digit
213 Radix Sort v2 Example Input: 61, 21, 32, 33, 65, 26, 49, 39 Output: 21, 26, 32, 33, 39, 49, 61,
214 Radix Sort Running Time Best / average / worst case: O( n + b k) n: Number of elements in input list b: Number of buckets k: Number of digits of largest number Usually, b n, so running time can be reduced to: O(nk) If k is also small (basically a constant), running time can be reduced to: O n Sorting Java ints: b=2, k = 32
215 Good luck next week.
Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix
Spring 2010 Review Topics Big O Notation Heaps Sorting Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix Hashtables Tree Balancing: AVL trees and DSW algorithm Graphs: Basic terminology and
More informationData Structures and Algorithms
Data Structures and Algorithms Session 24. Earth Day, 2009 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3137 Announcements Homework 6 due before last class: May 4th Final Review May
More informationCS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics
CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to
More informationWe can use a max-heap to sort data.
Sorting 7B N log N Sorts 1 Heap Sort We can use a max-heap to sort data. Convert an array to a max-heap. Remove the root from the heap and store it in its proper position in the same array. Repeat until
More informationCOMP Data Structures
COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort
More informationDr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions
Dr. Amotz Bar-Noy s Compendium of Algorithms Problems Problems, Hints, and Solutions Chapter 1 Searching and Sorting Problems 1 1.1 Array with One Missing 1.1.1 Problem Let A = A[1],..., A[n] be an array
More informationData Structures Brett Bernstein
Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of
More information2. 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.
1. T F: Consider a directed graph G = (V, E) and a vertex s V. Suppose that for all v V, there exists a directed path in G from s to v. Suppose that a DFS is run on G, starting from s. Then, true or false:
More informationCS350: Data Structures Dijkstra s Shortest Path Alg.
Dijkstra s Shortest Path Alg. James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Shortest Path Algorithms Several different shortest path algorithms exist
More informationSearching in General
Searching in General Searching 1. using linear search on arrays, lists or files 2. using binary search trees 3. using a hash table 4. using binary search in sorted arrays (interval halving method). Data
More informationThe ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value
The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 1 Possible implementations Sorted linear implementations o Appropriate if
More informationSorting and Selection
Sorting and Selection Introduction Divide and Conquer Merge-Sort Quick-Sort Radix-Sort Bucket-Sort 10-1 Introduction Assuming we have a sequence S storing a list of keyelement entries. The key of the element
More informationCSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014
CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up Nicki Dell Spring 2014 Final Exam As also indicated on the web page: Next Tuesday, 2:30-4:20 in this room Cumulative but
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree
More informationDIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS
CHAPTER 11 SORTING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND
More informationPractice Problems for the Final
ECE-250 Algorithms and Data Structures (Winter 2012) Practice Problems for the Final Disclaimer: Please do keep in mind that this problem set does not reflect the exact topics or the fractions of each
More information21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #
1. Prove that n log n n is Ω(n). York University EECS 11Z Winter 1 Problem Set 3 Instructor: James Elder Solutions log n n. Thus n log n n n n n log n n Ω(n).. Show that n is Ω (n log n). We seek a c >,
More informationCSci 231 Final Review
CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.
More informationSorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13
Sorting Hsuan-Tien Lin Dept. of CSIE, NTU June 9, 2014 H.-T. Lin (NTU CSIE) Sorting 06/09, 2014 0 / 13 Selection Sort: Review and Refinements idea: linearly select the minimum one from unsorted part; put
More informationSAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms
SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6 6.0 Introduction Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element
More informationR13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)
SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)
More informationSorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006
Sorting The Sorting Problem Input is a sequence of n items (a 1, a 2,, a n ) The mapping we want is determined by a comparison operation, denoted by Output is a sequence (b 1, b 2,, b n ) such that: {
More informationDesign and Analysis of Algorithms - - Assessment
X Courses» Design and Analysis of Algorithms Week 1 Quiz 1) In the code fragment below, start and end are integer values and prime(x) is a function that returns true if x is a prime number and false otherwise.
More informationAnnouncements Problem Set 4 is out!
CSC263 Week 8 Announcements Problem Set 4 is out! Due Tuesday (Nov 17) Other Announcements Drop date Nov 8 Final exam schedule is posted CSC263 exam Dec 11, 2-5pm This week s outline Graphs BFS Graph A
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17
601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 5.1 Introduction You should all know a few ways of sorting in O(n log n)
More informationCSE 373 NOVEMBER 8 TH COMPARISON SORTS
CSE 373 NOVEMBER 8 TH COMPARISON SORTS ASSORTED MINUTIAE Bug in Project 3 files--reuploaded at midnight on Monday Project 2 scores Canvas groups is garbage updated tonight Extra credit P1 done and feedback
More informationSorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort
Principles of Imperative Computation V. Adamchik CS 15-1 Lecture Carnegie Mellon University Sorting Sorting Sorting is ordering a list of objects. comparison non-comparison Hoare Knuth Bubble (Shell, Gnome)
More informationIS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)
IS 709/809: Computational Methods in IS Research Algorithm Analysis (Sorting) Nirmalya Roy Department of Information Systems University of Maryland Baltimore County www.umbc.edu Sorting Problem Given an
More informationSorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.
Sorting Sorting is ordering a list of elements. Types of sorting: There are many types of algorithms exist based on the following criteria: Based on Complexity Based on Memory usage (Internal & External
More informationIntroduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort
Introduction to Algorithms and Data Structures Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort Professor Ryuhei Uehara, School of Information Science, JAIST, Japan.
More informationUnit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33
Unit #4: Sorting CPSC : Basic Algorithms and Data Structures Anthony Estey, Ed Knorr, and Mehrdad Oveisi 0W Unit Outline Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity
More information07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31
Recap: Sorting 07 B: Sorting II CS1102S: Data Structures and Algorithms Martin Henz March 5, 2010 Generated on Friday 5 th March, 2010, 08:31 CS1102S: Data Structures and Algorithms 07 B: Sorting II 1
More informationCSE 100: GRAPH ALGORITHMS
CSE 100: GRAPH ALGORITHMS 2 Graphs: Example A directed graph V5 V = { V = E = { E Path: 3 Graphs: Definitions A directed graph V5 V6 A graph G = (V,E) consists of a set of vertices V and a set of edges
More informationPrelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader
Prelim 2 CS 2110, November 19, 2015, 7:30 PM 1 2 3 4 5 6 Total Question True Short Complexity Searching Trees Graphs False Answer Sorting Invariants Max 20 15 13 14 17 21 100 Score Grader The exam is closed
More informationPrelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader
Prelim 2 CS 2110, November 19, 2015, 7:30 PM 1 2 3 4 5 6 Total Question True Short Complexity Searching Trees Graphs False Answer Sorting Invariants Max 20 15 13 14 17 21 100 Score Grader The exam is closed
More informationAbout this exam review
Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review
More informationCSC 273 Data Structures
CSC 273 Data Structures Lecture 6 - Faster Sorting Methods Merge Sort Divides an array into halves Sorts the two halves, Then merges them into one sorted array. The algorithm for merge sort is usually
More informationCS240 Fall Mike Lam, Professor. Quick Sort
??!!!!! CS240 Fall 2015 Mike Lam, Professor Quick Sort Merge Sort Merge sort Sort sublists (divide & conquer) Merge sorted sublists (combine) All the "hard work" is done after recursing Hard to do "in-place"
More informationReal-world sorting (not on exam)
Real-world sorting (not on exam) Sorting algorithms so far Insertion sort Worst case Average case Best case O(n 2 ) O(n 2 ) O(n) Quicksort O(n 2 ) O(n log n) O(n log n) Mergesort O(n log n) O(n log n)
More informationQuestion 7.11 Show how heapsort processes the input:
Question 7.11 Show how heapsort processes the input: 142, 543, 123, 65, 453, 879, 572, 434, 111, 242, 811, 102. Solution. Step 1 Build the heap. 1.1 Place all the data into a complete binary tree in the
More informationCSE 373 Final Exam 3/14/06 Sample Solution
Question 1. (6 points) A priority queue is a data structure that supports storing a set of values, each of which has an associated key. Each key-value pair is an entry in the priority queue. The basic
More informationCSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING
CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING ASSORTED MINUTIAE HW6 Out Due next Wednesday ASSORTED MINUTIAE HW6 Out Due next Wednesday Only two late days allowed ASSORTED MINUTIAE HW6 Out Due
More informationCSC263 Week 8. Larry Zhang.
CSC263 Week 8 Larry Zhang http://goo.gl/forms/s9yie3597b Announcements (strike related) Lectures go as normal Tutorial this week everyone go to BA32 (T8, F2, F2, F3) Problem sets / Assignments are submitted
More information08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58
08 A: Sorting III CS1102S: Data Structures and Algorithms Martin Henz March 10, 2010 Generated on Tuesday 9 th March, 2010, 09:58 CS1102S: Data Structures and Algorithms 08 A: Sorting III 1 1 Recap: Sorting
More informationUnit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION
DESIGN AND ANALYSIS OF ALGORITHMS Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION http://milanvachhani.blogspot.in EXAMPLES FROM THE SORTING WORLD Sorting provides a good set of examples for analyzing
More informationSORTING AND SELECTION
2 < > 1 4 8 6 = 9 CHAPTER 12 SORTING AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016)
More informationOverview of Sorting Algorithms
Unit 7 Sorting s Simple Sorting algorithms Quicksort Improving Quicksort Overview of Sorting s Given a collection of items we want to arrange them in an increasing or decreasing order. You probably have
More informationFaster Sorting Methods
Faster Sorting Methods Chapter 9 Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Iterative Merge Sort Merge Sort in the Java Class Library Contents Quick Sort The Efficiency
More informationSORTING, SETS, AND SELECTION
CHAPTER 11 SORTING, SETS, AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM
More informationLecture Summary CSC 263H. August 5, 2016
Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.
More informationECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt
ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms C. L. Wyatt Today we will continue looking at sorting algorithms Bubble sort Insertion sort Merge sort Quick sort Common Sorting Algorithms
More informationSorting Pearson Education, Inc. All rights reserved.
1 19 Sorting 2 19.1 Introduction (Cont.) Sorting data Place data in order Typically ascending or descending Based on one or more sort keys Algorithms Insertion sort Selection sort Merge sort More efficient,
More informationCSE 373: Practice Final
CSE 373: Practice Final 1 Short Answer a) Provide two orderings [0,1,2,3,4,5,6,7] that are worst-case for quick sort. Assume that you select the first element as the pivot. Explain why this is the worst-case.
More informationProblem Score Maximum MC 34 (25/17) = 50 Total 100
Stony Brook University Midterm 2 CSE 373 Analysis of Algorithms November 22, 2016 Midterm Exam Name: ID #: Signature: Circle one: GRAD / UNDERGRAD INSTRUCTIONS: This is a closed book, closed mouth exam.
More informationCpt S 122 Data Structures. Sorting
Cpt S 122 Data Structures Sorting Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Sorting Process of re-arranging data in ascending or descending order Given
More informationProblem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.
Problem 5. Sorting Simple Sorting, Quicksort, Mergesort Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all 1 i j n. 98 99 Selection Sort
More informationAlgorithms and Applications
Algorithms and Applications 1 Areas done in textbook: Sorting Algorithms Numerical Algorithms Image Processing Searching and Optimization 2 Chapter 10 Sorting Algorithms - rearranging a list of numbers
More informationSorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?
Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n
More informationA loose end: binary search
COSC311 CRN 17281 - Session 20 (Dec. 3, 2018) A loose end: binary search binary search algorithm vs binary search tree A binary search tree, such as AVL, is a data structure. Binary search is an algorithm
More informationCS Data Structures and Algorithm Analysis
CS 483 - Data Structures and Algorithm Analysis Lecture VI: Chapter 5, part 2; Chapter 6, part 1 R. Paul Wiegand George Mason University, Department of Computer Science March 8, 2006 Outline 1 Topological
More informationPrelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader
Prelim 2 CS 2110, November 19, 2015, 5:30 PM 1 2 3 4 5 6 Total Question True Short Complexity Searching Trees Graphs False Answer Sorting Invariants Max 20 15 13 14 17 21 100 Score Grader The exam is closed
More informationChapter 9. Priority Queue
Chapter 9 Priority Queues, Heaps, Graphs Spring 2015 1 Priority Queue Priority Queue An ADT in which only the item with the highest priority can be accessed 2Spring 2015 Priority Depends on the Application
More informationCS 61BL Data Structures & Programming Methodology
CS 61BL Data Structures & Programming Methodology Summer 2018 Final Exam Solution This exam has 8 questions worth a total of 60 points and is to be completed in 110 minutes. The exam is closed book except
More informationData Structures Question Bank Multiple Choice
Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution
More informationINSTITUTE OF AERONAUTICAL ENGINEERING
INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2015S-10 Sorting David Galles Department of Computer Science University of San Francisco 10-0: Main Memory Sorting All data elements can be stored in memory at the
More informationPrelim 2, CS2110. SOLUTION
Prelim 2, CS2110. SOLUTION 7:30 PM, 25 April 2017 1. Name (1 point) Write your name and NetID at the top of every page of this exam. 2. Short Answer (26 points.) (a) Asymptotic complexity. 8 points. Be
More informationChapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1
Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset
More informationModule 2: Classical Algorithm Design Techniques
Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module
More informationAlgorithms and Data Structures (INF1) Lecture 7/15 Hua Lu
Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu Department of Computer Science Aalborg University Fall 2007 This Lecture Merge sort Quick sort Radix sort Summary We will see more complex techniques
More informationCS 310 Advanced Data Structures and Algorithms
CS 310 Advanced Data Structures and Algorithms Sorting June 13, 2017 Tong Wang UMass Boston CS 310 June 13, 2017 1 / 42 Sorting One of the most fundamental problems in CS Input: a series of elements with
More informationDivide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms
Divide and Conquer Algorithms: Advanced Sorting Prichard Ch. 10.2: Advanced Sorting Algorithms 1 Sorting Algorithm n Organize a collection of data into either ascending or descending order. n Internal
More informationPrelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer
Prelim 2 CS 2110, November 20, 2014, 7:30 PM 1 2 3 4 5 Extra Total Question True/False Short Answer Complexity Induction Trees Graphs Extra Credit Max 20 10 15 25 30 5 100 Score Grader The exam is closed
More informationHow many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.
Chapter 8. Sorting in Linear Time Types of Sort Algorithms The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. Quick Sort -- comparison-based
More informationGraphs & Digraphs Tuesday, November 06, 2007
Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements
More informationSorting Goodrich, Tamassia Sorting 1
Sorting Put array A of n numbers in increasing order. A core algorithm with many applications. Simple algorithms are O(n 2 ). Optimal algorithms are O(n log n). We will see O(n) for restricted input in
More informationMergesort again. 1. Split the list into two equal parts
Quicksort Mergesort again 1. Split the list into two equal parts 5 3 9 2 8 7 3 2 1 4 5 3 9 2 8 7 3 2 1 4 Mergesort again 2. Recursively mergesort the two parts 5 3 9 2 8 7 3 2 1 4 2 3 5 8 9 1 2 3 4 7 Mergesort
More informationCourse Review for Finals. Cpt S 223 Fall 2008
Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,
More informationLab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018
CS18 Integrated Introduction to Computer Science Fisler, Nelson Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018 Contents 1 Heapsort 2 2 Quicksort 2 3 Bubble Sort 3 4 Merge Sort 3 5 Mirror Mirror
More informationMultiple-choice (35 pt.)
CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need
More informationPractical Session #11 - Sort properties, QuickSort algorithm, Selection
Practical Session #11 - Sort properties, QuickSort algorithm, Selection Quicksort quicksort( A, low, high ) if( high > low ) pivot partition( A, low, high ) // quicksort( A, low, pivot-1 ) quicksort( A,
More informationCPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)
CPSC 311 Lecture Notes Sorting and Order Statistics (Chapters 6-9) Acknowledgement: These notes are compiled by Nancy Amato at Texas A&M University. Parts of these course notes are based on notes from
More informationYour favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY
Course Code : BCS-042 Course Title : Introduction to Algorithm Design Assignment Number : BCA(IV)-042/Assign/14-15 Maximum Marks : 80 Weightage : 25% Last Date of Submission : 15th October, 2014 (For July
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree
More informationAlgorithms: Design & Practice
Algorithms: Design & Practice Deepak Kumar Bryn Mawr College Spring 2018 Course Essentials Algorithms Design & Practice How to design Learn some good ones How to implement practical considerations How
More informationDO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.
CS61B Fall 2011 UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division Test #2 Solutions P. N. Hilfinger 1. [3 points] Consider insertion sort, merge
More informationChapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Decrease-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance
More informationComputer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms
Computer Science 385 Design and Analysis of Algorithms Siena College Spring 2019 Topic Notes: Brute-Force Algorithms Our first category of algorithms are called brute-force algorithms. Levitin defines
More informationSolutions to relevant spring 2000 exam problems
Problem 2, exam Here s Prim s algorithm, modified slightly to use C syntax. MSTPrim (G, w, r): Q = V[G]; for (each u Q) { key[u] = ; key[r] = 0; π[r] = 0; while (Q not empty) { u = ExtractMin (Q); for
More informationMA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review DFS, BFS Topological Sort MA/CSSE 473 Day 12 Questions? Interpolation Search Insertion sort analysis Depth first Search Breadth first
More informationCSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014
CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting Aaron Bauer Winter 2014 The main problem, stated carefully For now, assume we have n comparable elements in an array and we want
More informationTotal Score /1 /20 /41 /15 /23 Grader
NAME: NETID: CS2110 Spring 2015 Prelim 2 April 21, 2013 at 5:30 0 1 2 3 4 Total Score /1 /20 /41 /15 /23 Grader There are 5 questions numbered 0..4 on 8 pages. Check now that you have all the pages. Write
More informationCS8391-DATA STRUCTURES QUESTION BANK UNIT I
CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those
More informationCS302 Data Structures using C++
CS302 Data Structures using C++ Study Guide for the Final Exam Fall 2018 Revision 1.1 This document serves to help you prepare towards the final exam for the Fall 2018 semester. 1. What topics are to be
More informationCOMP 251 Winter 2017 Online quizzes with answers
COMP 251 Winter 2017 Online quizzes with answers Open Addressing (2) Which of the following assertions are true about open address tables? A. You cannot store more records than the total number of slots
More informationLecture 8: Mergesort / Quicksort Steven Skiena
Lecture 8: Mergesort / Quicksort Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Give an efficient
More informationGraphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131
Graphs Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - Faner 3131 1 Outline Introduction to Graphs Graph Traversals Finding a
More informationData structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018
Data structures More sorting Dr. Alex Gerdes DIT961 - VT 2018 Divide and conquer Very general name for a type of recursive algorithm You have a problem to solve: - Split that problem into smaller subproblems
More informationQuick Sort. CSE Data Structures May 15, 2002
Quick Sort CSE 373 - Data Structures May 15, 2002 Readings and References Reading Section 7.7, Data Structures and Algorithm Analysis in C, Weiss Other References C LR 15-May-02 CSE 373 - Data Structures
More information1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i
Selection Sort Algorithm Principles of Computer Science II Sorting Algorithms This algorithm first finds the smallest element in the array and exchanges it with the element in the first position, then
More information