1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

Size: px
Start display at page:

Download "1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list"

Transcription

1 Algorithms One of the more challenging aspects of Computer Science are algorithms. An algorithm is a plan that solves a problem. When assembling a bicycle based on the included instructions, in this case, the instructions are the algorithm. The expression goes, There are many ways to skin a cat, meaning there are multiple ways to solve pretty much any problem. The key is, to find the best algorithm or solution for a specific problem. Let s take a typical problem. I drop my contact list, consisting of a bunch of cards, on the floor, and now must pick them up and put them back in alphabetical order. What is the quickest way to do this? See figure below: Sorting a list of cards Insertion Sort Most people will take all the cards in their right hand and slowly build a sorted list in their left hand (or in one hand having my finger separate between the sorted and the unsorted list). As we move one card at a time from the unsorted to sorted list, we place the new card in its correct location in the sorted list, But, just how do we find the correct location? Well since we are inserting into a sorted list, we will see that we can find the correct spot using a binary search. Alternatively, we can do a simple traversal as we will now demonstrate. Assuming the list contains

2 the following numbers, 5,9,2,7,6,10,4,3,8,1. We will build a sorted list one number at a time. See below. Note: We have not specified how we find the location for the new number. Insertion Sort = Unsorted ---- = Sorted STEP LIST COMMENT Start 5,9,2,7,6,10,4,3,8,1 1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list 2 5,9,2,7,6,10,4,3,8,1 The next unsorted (9) is inserted. It is greater than the last sorted number so it becomes last number of the sorted list. 3 2,5,9,7,6,10,4,3,8,1 The next unsorted (2) is inserted. It is less than 9 and 5 so they are shifted right and 2 is placed in the beginning. 4 2,5,7,9,6,10,4,3,8,1 7 is added and 9 is shifted one place 10 1,2,3,4,5,6,7,89,10 List is sorted after tenth insertion Bubble Sort Another method is to find the card that should be last, e.g. it has the highest number, and place that last. Next we the next highest card and place that before the highest card and we slowly build a sorted list from highest to lowest. Each time we need to find a new card we must traverse the whole list of unsorted cards. A variation of this method is Bubble Sort. With the Bubble Sort algorithm, we do indeed always have the next highest card after each pass through the list,

3 but we bubble this value up. By bubbling, we partially sort all the numbers as we find the highest number. To explain, we ll use a list with ten numbers that require sorting. The list is 5,9,2,7,6,10,4,3,8,1. With Bubble Sort, we look only two numbers at time. First we examine the first two numbers, then the second and third, and so on until we have examined the ninth and tenth. Each time we examine two numbers, we see if they are in order, if they aren t we switch them. The first time through the list, the highest number will bubble out, and the second time the next highest number will bubble out. Each time we go through the list one less time, as our unsorted numbers decreased by one each time. We have a sorted list when we loop through the unsorted list and no switches have to be made. In the example below, the unsorted portion of the list is blue and the sorted portion is green = Unsorted ---- = Sorted Start - 5,9,2,7,6,10,4,3,8,1 Pass 1-9,2,5,6,7,4,3,8,1,10 Bubble Highest Pass 2-2,5,6,7,4,3,8,1,9,10 Bubble Next Highest Pass 3-2,5,6,4,3,7,1,8,9,10 Bubble Next Highest Pass 4-2,5,4,3,6,1,7,8,9,10 Bubble Next Highest Pass 5-2,4,3,5,1,6,7,8,9,10 Bubble Next Highest Pass 6-2,3,4,1,5,6,7,8,9,10 Bubble Next Highest Pass 7-2,3,1,4,5,6,7,8,9,10 Bubble Next Highest Pass 8-2,1,3,4,5,6,7,8,9,10 Bubble Next Highest Pass 9-1,2,3,4,5,6,7,8,9,10 Bubble Next Highest Alternating Bubble Sort As you can see Passes 6 through 9 are only to move the number one to the first position. Imagine if we had a million numbers all in order except the last one was a one, we would require a million passes just to sort one number! Below, I have looked to see what would happen if we Bubble Sort alternately from left to right,

4 to first find the highest number, and then from right to left to find the lowest number. As you can see, we have cut down the number of passes to from 9 to = Unsorted ---- = Sorted Start - 5,9,2,7,6,10,4,3,8,1 Pass 1 5,2,7,6,9,4,3,8,1,10 Bubble Highest Pass 2 1,5,2,7,6,9,4,3,8,10 Bubble Lowest Pass 3-1,2,5,6,7,4,3,8,9,10 Bubble Next Highest Pass 4-1,2,3,5,6,7,4,8,9,10 Bubble Next Lowest Pass 5-1,2,3,5,6,4,7,8,9,10 Bubble Next Highest Pass 6-1,2,3,4,5,6,7,8,9,10 Bubble Next Lowest Partition Sort This algorithm built around the concept that we can quickly sort something if we continuously partially sort the list of items to be sorted. If we partition a list into three parts, the part less than the partition, the partition, and the list of items larger than the partition. One of the nice things about partition sort is, after I partition a list I have two lists, one whose members are all less than the partition and one whose members are larger. Therefore, I can safely sort each list independently of the other and combine them together for one sorted list. This has huge implications. Nowadays, most computers have multiple processors usually four. A well written Partition Sort will take advantage of this, and will be able to sort four times as fast! For example, if we take the list 5,9,2,7,6,10,4,3,8,1, we can divide it into 3 parts, those numbers that are less than 6, the number 6, and the numbers greater than 6. See below.

5 Left Side Right Side Partition {5,9,2,7,6,10,4,3,8,1} = {5,2,4,3,1} {9,7,10,8} Each list in turn can again be further partioned. {2,1} {5,4} {} {9,10,8} And again. {} {2} {} {5} {} {8} {10} Merge Sort Merge Sort uses the fact that it is relatively easy to combine two lists if each list is already sorted. So we take our list, break it down to the smallest possible lists and then recombine them. What makes merge sort of interest, is that the number of steps required to sort the list is always known. It is a function of the size of the list. Specifically, it is Log2(N). So where N=10, we will need only 3 steps to recombine. The example below illustrates how Merge Sort works. {5,9,2,7,6,10,4,3,8,1} Original List {5,9} {2,7} {6,10} {3,4} {1,8} Step one break into lists of 1 or two 2 sorted items. {2,5,79} {3,4,6,10} {1,8} Step two recombine. {2,3,4,5,6,7,9,10} {1,8} Step three recombine. {1,2,3,4,5,6,7,8,9,10} Step four recombine. Comparing Algorithms The major aspect of any algorithm, is of course, the likelihood that our algorithm will be correct (or very close to correct). In most cases the assumption is that every algorithm compared will be 100% correct 100% of the time. The second aspect of algorithms is how long will this algorithm take to complete. To make comparisons of algorithms we usually say algorithm speed is equivalent to the number of computer operations needed by the algorithm to complete in the best, average, and worst case scenarios. Best and worst case scenarios are cases where the data analyzed affects the algorithm speed. For example, the

6 Bubble Sort algorithm will take very little time when the data is already sorted while Merge Sort will always take the same time any type of data. So to compare apples to apples, we usually have worst case, best case, and average case scenarios for algorithms. In fact, one interesting algorithm is; Start with Bubble Sort, if the data is a worst case scenario, switch to a sort which works well with all scenarios, like a Merge Sort. Big O Notation When we compare modes of transportation, we create categories. For example, we would never compare speed of a car with speed of an airplane. We may make speed categories like runner, four wheels, airplane, rocket ship, speed of light. Now let s take two runners, Runner A can run the mile in five minutes and Runner B can run a mile in 20 minutes. So we say, Runner A is four times faster than Runner B. Compared to an airplane, however, neither runner can be compared! People and airplanes are in two completely different leagues. To bring this comparison to algorithms, we must keep in mind that computers perform over one billion calculations a second. So Algorithm A may be four times faster than Algorithm B, but in the grand scheme of things, this isn t such a big deal. For example, 10 million operations or 40 million operations doesn t have much effect on a processor running at one billion calculations a second. What does makes an algorithm much slower is exponential growth, for example, number of calculations is N^2. This makes the difference whether an algorithm is a runner or an airplane. For example, if we sort a million numbers using bubble sort, in the worst case scenario it will make N^2 or one trillion operations. Merge sort will take 20 million operations. Translating this into computer time (at 1 billion calculations a second) we get ½ an hour vs less than 1 second. Another point, when discussing algorithms, is their order of growth. That is, to go one block, we don t really care if we walk, drive, or fly. The time differential is very small. What we are interested in is what happens if we have to travel 1000 miles. So what we are really interested in is the order of growth as the problem size increases. In Computer Science, this is known as Bog O Notation, with O referring to the Order of growth.

7 Below are common big O values. Big O classes in order of growth (from Big O Growth Name O(1) O(log(n)) O((log(n))^c) O(n) O(n^2) O(n^c) O(c^n) constant logarithmic polylogarithmic linear quadratic polynomial exponential Hard Problems Hard problems are those problems that can only be solved through brute force, there is no quick way to solve them. The most famous example of a hard problem is the Traveling Salesman Problem (or TSP). The Traveling Salesman Problem is as follows. A salesman needs to visit many cities and then return to his home city. Naturally, he would like to do this in the least amount of time, so he can return home and get more work done. Let s say the salesman wishes to visit 20 cities. On way to figure out the fastest route is to examine every possible combination of paths, and find which one is the shortest. Is this a good solution? Well to visit twenty cities there are 20 (20!) factorial possibilities which is equal to 2,432,902,008,176,640,000. A computer running at one billion calculations per second would take around 15 years to find the shortest route. Graphs

8 As we have mentioned elsewhere, many common situations can be represented as a graph. For example, the internet, tasks to be done, and maps can all be represented as graphs. So algorithms that deal with graphs will prove very useful. Our first problem is; We need to visit every vertex on the graph, only once, how do we accomplish this? We will cover two ways to conduct this traversal. Depth First Search (DFS) or Breadth First Search (BFS). Undirected Graph Depth First Search (DFS) In the figure above, we have Vertices A F. Let s pick A as the start vertex. The DFS then goes something like this: 1) Find the first unvisited ver Search Algorithms Searching for data If we are looking at disorganized data, there is only one way to find what we are looking for. That is, we look sequentially at the first item, and then the second item, etc. For example, if we go to a garage sale and are looking through a bunch of old baseball cards we go through them one by one. Or if we have a bunch of papers in a drawer we look for the document we are interested, paper by paper.

9 On the other hand, if we are looking for a pair of clean socks, in our bedroom, we know to first look in the sock drawer. That is because the clothes are organized. The same thing with data. If the data is organized, then we can perform an intelligent search. What are some intelligent ways to store data? Sorted array is probably the quickest to search but adding data is costly because we have to rearrange the data to always be sorted. Another data structure is a binary search tree. In this case the root has the middle value and all the children to a parents left are smaller or equal than their parent. The advantage of a binary tree is that adding items is pretty quick but the tree can become unbalanced and have to be rebalanced. The search time for either search is Binary Search Log(n) 32 nodes 5 (worst case) Searching for a path Sometimes I am not looking for a single element. Rather I am looking for a path. GPS - I am looking for the shortest/quickest path between two addresses. Traveling Salesman (TSP) - I am looking for the total shortest/quickest path between many cities. See TSP link for more about this problem. LinkedIn - I am looking for the total shortest/quickest connection between two people. These cases can be represented as graphs. A graph is basically a collection vertices and edges that connect them. In the first two examples, there is also associated with each edge, a value denoting the cost associated with each edge. The challenge with graph problems is that there are often millions of possibilities. Dijkstra's algorithm - calculates the shortest path.

10 Sorting algorithms Below are examples of sorting algorithms. They can be used to sort any object for which we can determine a greater than, less than, and equal operation. For simplicity, the examples below are using whole numbers or integers and we are sorting lowest to highest. Quick Sort (Partition Sort) Pick a value. Assume it is the middle value. Move everything lower to its left and everything larger to its right. Then do the same for the left hand side and the right hand side. Merge Sort Step A If there are only one member in the list then it is sorted. If there are two members, then switch them and the list is sorted. We are done sorting. Step B Break down list into two sorted child lists and then recombine. To create sorted child lists, divide list onto two halves and go to Step A. Insertion Sort Starts with a new list which is sorted, or an empty list. Taking one element at a time from the unsorted list, and add to the sorted list. A sorted list is built. Note: because list is always sorted, adding a new element can be done faster. Can you explain why? Bubble Sort This sort after the following property. The highest number is guaranteed to bubble out after the first pass. The second highest number is guaranteed to bubble out after the second pass and so on.

11 Each pass through the list, each element is compared with the element after it. If the first number is greater, then the two numbers are switched. The list is sorted after a pass has been made in which there are no switches. -first_search Depth-first search (DFS) for undirected graphs Depth-first search, or DFS, is a way to traverse the graph. The principle of the algorithm is quite simple: to go forward (in depth) while there is such possibility, otherwise to backtrack. Algorithm In DFS, each vertex has three possible colors representing its state: white: vertex is unvisited; gray: vertex is in progress; black: DFS has finished processing the vertex. NB. For most algorithms boolean classification unvisited / visited is quite enough, but we show general case here. Initially all vertices are white (unvisited). DFS starts in arbitrary vertex and runs as follows: 1. Mark vertex u as gray (visited). 2. For each edge (u, v), where u is white, run depth-first search for u recursively. 3. Mark vertex u as black and backtrack to the parent. Example. Traverse a graph shown below, using DFS. Start from a vertex with number 1.

12 Source graph. Mark a vertex 1 as gray. There is an edge (1, 4) and a vertex 4 is unvisited. Go there.

13 Mark the vertex 4 as gray. There is an edge (4, 2) and vertex a 2 is unvisited. Go there. Mark the vertex 2 as gray.

14 There is an edge (2, 5) and a vertex 5 is unvisited. Go there. Mark the vertex 5 as gray. There is an edge (5, 3) and a vertex 3 is unvisited. Go there.

15 Mark the vertex 3 as gray. There are no ways to go from the vertex 3. Mark it as black and backtrack to the vertex 5. There is an edge (5, 4), but the vertex 4 is gray.

16 There are no ways to go from the vertex 5. Mark it as black and backtrack to the vertex 2. There are no more edges, adjacent to vertex 2. Mark it as black and backtrack to the vertex 4. There is an edge (4, 5), but the vertex 5 is black.

17 There are no more edges, adjacent to the vertex 4. Mark it as black and backtrack to the vertex 1. There are no more edges, adjacent to the vertex 1. Mark it as black. DFS is over. As you can see from the example, DFS doesn't go through all edges. The vertices and edges, which depth-first search has visited is a tree. This tree contains all vertices of the graph (if it is connected) and is called graph spanning tree. This tree exactly corresponds to the recursive calls of DFS.

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

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach CSC 8301- Design and Analysis of Algorithms Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms Brute-Force Approach Brute force is a straightforward approach to solving a problem, usually

More information

DESIGN AND ANALYSIS OF ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Module 1 OBJECTIVE: Algorithms play the central role in both the science and the practice of computing. There are compelling reasons to study algorithms.

More information

Recitation 9. Prelim Review

Recitation 9. Prelim Review Recitation 9 Prelim Review 1 Heaps 2 Review: Binary heap min heap 1 2 99 4 3 PriorityQueue Maintains max or min of collection (no duplicates) Follows heap order invariant at every level Always balanced!

More information

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I MCA 312: Design and Analysis of Algorithms [Part I : Medium Answer Type Questions] UNIT I 1) What is an Algorithm? What is the need to study Algorithms? 2) Define: a) Time Efficiency b) Space Efficiency

More information

Figure 1: A directed graph.

Figure 1: A directed graph. 1 Graphs A graph is a data structure that expresses relationships between objects. The objects are called nodes and the relationships are called edges. For example, social networks can be represented as

More information

Parallel Pointers: Graphs

Parallel Pointers: Graphs Parallel Pointers: Graphs Breadth first Search (BFS) 101 Given a vertex S, what is the distance of all the other vertices from S? Why would this be useful? To find the fastest route through a network.

More information

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS Analyzing find in B-trees Since the B-tree is perfectly height-balanced, the worst case time cost for find is O(logN) Best case: If every internal node is completely

More information

UNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,

More information

Computer 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 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 information

CS61BL. Lecture 5: Graphs Sorting

CS61BL. Lecture 5: Graphs Sorting CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)

More information

Sorting and Selection

Sorting 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 information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

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

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review 1 Review 1 Something I did not emphasize enough last time is that during the execution of depth-firstsearch, we construct depth-first-search trees. One graph may have multiple depth-firstsearch trees,

More information

Trees. Arash Rafiey. 20 October, 2015

Trees. Arash Rafiey. 20 October, 2015 20 October, 2015 Definition Let G = (V, E) be a loop-free undirected graph. G is called a tree if G is connected and contains no cycle. Definition Let G = (V, E) be a loop-free undirected graph. G is called

More information

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29 Lecture 13 Connectedness in graphs Spanning trees in graphs Finding a minimal spanning tree Time costs of graph problems and NP-completeness Finding a minimal spanning tree: Prim s and Kruskal s algorithms

More information

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 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 information

Outline. Graphs. Divide and Conquer.

Outline. Graphs. Divide and Conquer. GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.

More information

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the

More information

looking ahead to see the optimum

looking ahead to see the optimum ! Make choice based on immediate rewards rather than looking ahead to see the optimum! In many cases this is effective as the look ahead variation can require exponential time as the number of possible

More information

Lecture Summary CSC 263H. August 5, 2016

Lecture 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 information

(Refer Slide Time: 02.06)

(Refer Slide Time: 02.06) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 27 Depth First Search (DFS) Today we are going to be talking

More information

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302 Topics VCU, Department of Computer Science CMSC 302 Trees Vojislav Kecman Terminology Trees as Models Some Tree Theorems Applications of Trees Binary Search Tree Decision Tree Tree Traversal Spanning Trees

More information

Sorting. Dr. Baldassano Yu s Elite Education

Sorting. Dr. Baldassano Yu s Elite Education Sorting Dr. Baldassano Yu s Elite Education Last week recap Algorithm: procedure for computing something Data structure: system for keeping track for information optimized for certain actions Good algorithms

More information

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution

More information

Intro to Algorithms. Professor Kevin Gold

Intro to Algorithms. Professor Kevin Gold Intro to Algorithms Professor Kevin Gold What is an Algorithm? An algorithm is a procedure for producing outputs from inputs. A chocolate chip cookie recipe technically qualifies. An algorithm taught in

More information

Minimum spanning trees

Minimum spanning trees Carlos Moreno cmoreno @ uwaterloo.ca EI-3 https://ece.uwaterloo.ca/~cmoreno/ece5 Standard reminder to set phones to silent/vibrate mode, please! During today's lesson: Introduce the notion of spanning

More information

Algorithm Analysis. Performance Factors

Algorithm Analysis. Performance Factors Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases Every algorithm has certain inputs that allow it

More information

Measuring algorithm efficiency

Measuring algorithm efficiency CMPT 225 Measuring algorithm efficiency Timing Counting Cost functions Cases Best case Average case Worst case Searching Sorting O Notation O notation's mathematical basis O notation classes and notations

More information

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario Coping with the Limitations of Algorithm Power Tackling Difficult Combinatorial Problems There are two principal approaches to tackling difficult combinatorial problems (NP-hard problems): Use a strategy

More information

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02) Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 04 Lecture - 01 Merge Sort (Refer

More information

Ideally your algorithms for both parts should run in linear time. You will receive partial credit for a polynomial-time algorithm.

Ideally your algorithms for both parts should run in linear time. You will receive partial credit for a polynomial-time algorithm. HW 7: Extra problems Instructor: Sariel Har-Peled CS/ECE 374: Algorithms & Models of Computation, Fall 2017 Version: 1.0 1 Consider a directed graph G, where each edge is colored either red, white, or

More information

LECTURE 26 PRIM S ALGORITHM

LECTURE 26 PRIM S ALGORITHM DATA STRUCTURES AND ALGORITHMS LECTURE 26 IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD STRATEGY Suppose we take a vertex Given a single vertex v 1, it forms a minimum spanning tree on one

More information

TIE Graph algorithms

TIE Graph algorithms TIE-20106 239 11 Graph algorithms This chapter discusses the data structure that is a collection of points (called nodes or vertices) and connections between them (called edges or arcs) a graph. The common

More information

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

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 information

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

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered

More information

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

SAMPLE 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 information

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

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10 CS 220: Discrete Structures and their Applications graphs zybooks chapter 10 directed graphs A collection of vertices and directed edges What can this represent? undirected graphs A collection of vertices

More information

Analysis of algorithms

Analysis of algorithms Analysis of algorithms Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing a

More information

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017 1 Spanning Trees, greedy algorithms Lecture 22 CS2110 Fall 2017 1 We demo A8 Your space ship is on earth, and you hear a distress signal from a distance Planet X. Your job: 1. Rescue stage: Fly your ship

More information

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 20 Priority Queues Today we are going to look at the priority

More information

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF

More information

CSCE 350: Chin-Tser Huang. University of South Carolina

CSCE 350: Chin-Tser Huang. University of South Carolina CSCE 350: Data Structures and Algorithms Chin-Tser Huang huangct@cse.sc.edu University of South Carolina Announcement Homework 2 will be returned on Thursday; solution will be available on class website

More information

The 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 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 information

Algorithms Interview Questions

Algorithms Interview Questions Algorithms Interview Questions 1. Define the concept of an algorithm. An algorithm is any well-defined computational procedure that takes some value (or set of values) as input and produces some value

More information

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT PROJECT 3 500 Internal Error problems Hopefully all resolved (or close to) P3P1 grades are up (but muted) Leave canvas comment Emails tomorrow End of quarter GRAPHS

More information

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

Chapter 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 information

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

CSI 604 Elementary Graph Algorithms

CSI 604 Elementary Graph Algorithms CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the

More information

Data Structures Brett Bernstein

Data 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 information

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

& ( D.  mnp ' ( ) n 3. n 2. ( ) C.  n CSE Name Test Summer Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n " n matrices is: A. " n C. "% n B. " max( m,n, p). The

More information

Brute Force: Selection Sort

Brute Force: Selection Sort Brute Force: Intro Brute force means straightforward approach Usually based directly on problem s specs Force refers to computational power Usually not as efficient as elegant solutions Advantages: Applicable

More information

Introduction to Computer Science

Introduction to Computer Science Introduction to Computer Science CSCI 109 Readings St. Amant, Ch. 4, Ch. 8 China Tianhe-2 Andrew Goodney Spring 2018 An algorithm (pronounced AL-go-rithum) is a procedure or formula for solving a problem.

More information

Module 2: Classical Algorithm Design Techniques

Module 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 information

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity. 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 information

Lecture 3. Brute Force

Lecture 3. Brute Force Lecture 3 Brute Force 1 Lecture Contents 1. Selection Sort and Bubble Sort 2. Sequential Search and Brute-Force String Matching 3. Closest-Pair and Convex-Hull Problems by Brute Force 4. Exhaustive Search

More information

Lecture 3: Graphs and flows

Lecture 3: Graphs and flows Chapter 3 Lecture 3: Graphs and flows Graphs: a useful combinatorial structure. Definitions: graph, directed and undirected graph, edge as ordered pair, path, cycle, connected graph, strongly connected

More information

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3 CPSC 320: Intermediate Algorithm Design and Analysis Author: Susanne Bradley Tutorial: Week 3 At the time of this week s tutorial, we were approaching the end of our stable matching unit and about to start

More information

Analysis of Algorithms. 5-Dec-16

Analysis of Algorithms. 5-Dec-16 Analysis of Algorithms 5-Dec-16 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing

More information

6.001 Notes: Section 31.1

6.001 Notes: Section 31.1 6.001 Notes: Section 31.1 Slide 31.1.1 In previous lectures we have seen a number of important themes, which relate to designing code for complex systems. One was the idea of proof by induction, meaning

More information

CSCI E 119 Section Notes Section 11 Solutions

CSCI E 119 Section Notes Section 11 Solutions CSCI E 119 Section Notes Section 11 Solutions 1. Double Hashing Suppose we have a 7 element hash table, and we wish to insert following words: apple, cat, anvil, boy, bag, dog, cup, down We use hash functions:

More information

Graphs and Algorithms

Graphs and Algorithms Graphs and Algorithms Graphs are a mathematical concept readily adapted into computer programming. Graphs are not just data structures, that is, they are not solutions to simple data storage problems.

More information

CS6301 Programming and Data Structures II Unit -5 REPRESENTATION OF GRAPHS Graph and its representations Graph is a data structure that consists of following two components: 1. A finite set of vertices

More information

Throughout this course, we use the terms vertex and node interchangeably.

Throughout this course, we use the terms vertex and node interchangeably. Chapter Vertex Coloring. Introduction Vertex coloring is an infamous graph theory problem. It is also a useful toy example to see the style of this course already in the first lecture. Vertex coloring

More information

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

Multiple-choice (35 pt.)

Multiple-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 information

Lecture 15: Algorithms. AP Computer Science Principles

Lecture 15: Algorithms. AP Computer Science Principles Lecture 15: Algorithms AP Computer Science Principles Algorithm algorithm: precise sequence of instructions to solve a computational problem. Search for a name in a phone s contact list. Sort emails by

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue

More information

Algorithm classification

Algorithm classification Types of Algorithms Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We ll talk about a classification scheme for algorithms This classification scheme

More information

(Refer Slide Time: 05:25)

(Refer Slide Time: 05:25) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering IIT Delhi Lecture 30 Applications of DFS in Directed Graphs Today we are going to look at more applications

More information

Principles of Algorithm Design

Principles of Algorithm Design Principles of Algorithm Design When you are trying to design an algorithm or a data structure, it s often hard to see how to accomplish the task. The following techniques can often be useful: 1. Experiment

More information

Analysis of Algorithms

Analysis of Algorithms Algorithm An algorithm is a procedure or formula for solving a problem, based on conducting a sequence of specified actions. A computer program can be viewed as an elaborate algorithm. In mathematics and

More information

TIE Graph algorithms

TIE Graph algorithms TIE-20106 1 1 Graph algorithms This chapter discusses the data structure that is a collection of points (called nodes or vertices) and connections between them (called edges or arcs) a graph. The common

More information

O(n): printing a list of n items to the screen, looking at each item once.

O(n): printing a list of n items to the screen, looking at each item once. UNIT IV Sorting: O notation efficiency of sorting bubble sort quick sort selection sort heap sort insertion sort shell sort merge sort radix sort. O NOTATION BIG OH (O) NOTATION Big oh : the function f(n)=o(g(n))

More information

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville Computer Science and Software Engineering University of Wisconsin - Platteville 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville Read: Textbook Chapter 3.7-3.9,3.12, 4. Problem Solving as

More information

CISC

CISC CISC-235 20180115+17+19 Much of the material we covered this week was already posted in the notes for last week. These notes take up where those left off, and fill in some gaps. We have discussed the notation

More information

CAD Algorithms. Categorizing Algorithms

CAD Algorithms. Categorizing Algorithms CAD Algorithms Categorizing Algorithms Mohammad Tehranipoor ECE Department 2 September 2008 1 Categorizing Algorithms Greedy Algorithms Prim s Algorithm (Minimum Spanning Tree) A subgraph that is a tree

More information

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

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes Graphs Graphs A graph is a data structure consisting of nodes (or vertices) and edges An edge is a connection between two nodes A D B E C Nodes: A, B, C, D, E Edges: (A, B), (A, D), (D, E), (E, C) Nodes

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

More information

(Refer Slide Time: 01:00)

(Refer Slide Time: 01:00) Advanced Operations Research Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology, Madras Lecture minus 26 Heuristics for TSP In this lecture, we continue our discussion

More information

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

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is

More information

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

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1 Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching

More information

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F)) DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]

More information

Sorting is a problem for which we can prove a non-trivial lower bound.

Sorting is a problem for which we can prove a non-trivial lower bound. Sorting The sorting problem is defined as follows: Sorting: Given a list a with n elements possessing a total order, return a list with the same elements in non-decreasing order. Remember that total order

More information

CS302 Data Structures using C++

CS302 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 information

L.J. Institute of Engineering & Technology Semester: VIII (2016)

L.J. Institute of Engineering & Technology Semester: VIII (2016) Subject Name: Design & Analysis of Algorithm Subject Code:1810 Faculties: Mitesh Thakkar Sr. UNIT-1 Basics of Algorithms and Mathematics No 1 What is an algorithm? What do you mean by correct algorithm?

More information

Chapter 3. The Efficiency of Algorithms INVITATION TO. Computer Science

Chapter 3. The Efficiency of Algorithms INVITATION TO. Computer Science Chapter 3 The Efficiency of Algorithms INVITATION TO 1 Computer Science Objectives After studying this chapter, students will be able to: Describe algorithm attributes and why they are important Explain

More information

Module 6 NP-Complete Problems and Heuristics

Module 6 NP-Complete Problems and Heuristics Module 6 NP-Complete Problems and Heuristics Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu P, NP-Problems Class

More information

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan 1 General Graph Search In general terms, the generic graph search algorithm looks like the following: def GenerateGraphSearchTree(G, root):

More information

Algorithms: Lecture 10. Chalmers University of Technology

Algorithms: Lecture 10. Chalmers University of Technology Algorithms: Lecture 10 Chalmers University of Technology Today s Topics Basic Definitions Path, Cycle, Tree, Connectivity, etc. Graph Traversal Depth First Search Breadth First Search Testing Bipartatiness

More information

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w Chapter 7 Trees 7.1 Introduction A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w Tree Terminology Parent Ancestor Child Descendant Siblings

More information

Search Lesson Outline

Search Lesson Outline 1. Searching Lesson Outline 2. How to Find a Value in an Array? 3. Linear Search 4. Linear Search Code 5. Linear Search Example #1 6. Linear Search Example #2 7. Linear Search Example #3 8. Linear Search

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 06 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Graphs Definition Terminology two ways to represent edges in implementation traversals

More information

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms Chapter 3 The Efficiency of Algorithms Objectives INVITATION TO Computer Science 1 After studying this chapter, students will be able to: Describe algorithm attributes and why they are important Explain

More information

Assignment No 2 (Group B)

Assignment No 2 (Group B) Assignment No 2 (Group B) 1 Problem Statement : Concurrent Implementation of Travelling Salesman Problem. 2 Objective : To develop problem solving abilities using Mathematical Modeling. To apply algorithmic

More information

Seach algorithms The travelling salesman problem The Towers of Hanoi Playing games. Comp24412: Symbolic AI. Lecture 4: Search. Ian Pratt-Hartmann

Seach algorithms The travelling salesman problem The Towers of Hanoi Playing games. Comp24412: Symbolic AI. Lecture 4: Search. Ian Pratt-Hartmann Comp24412: Symbolic AI Lecture 4: Search Ian Pratt-Hartmann Room KB2.38: email: ipratt@cs.man.ac.uk 2016 17 Outline Seach algorithms The travelling salesman problem The Towers of Hanoi Playing games Typical

More information

Dr. 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 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 information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 10: Search and Heaps MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Search and Heaps 2 Linear Search Binary Search Introduction to trees Priority Queues Heaps Linear Search

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours TED (10)-3071 Reg. No.. (REVISION-2010) (Maximum marks: 100) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours PART

More information

OCR H446 A-Level Computer Science

OCR H446 A-Level Computer Science Name: Class Teacher: Date: OCR H446 A-Level Computer Science REVISION BOOKLET 2.3 ALGORITHMS Content in H446 A-Level Computer Science: 1.1 The characteristics of contemporary processors, input, output

More information

CS 4349 Lecture October 18th, 2017

CS 4349 Lecture October 18th, 2017 CS 4349 Lecture October 18th, 2017 Main topics for #lecture include #minimum_spanning_trees. Prelude Homework 6 due today. Homework 7 due Wednesday, October 25th. Homework 7 has one normal homework problem.

More information