CSCI 3110 Algorithms I
Summer 2016, Dalhousie University
Course website: http:/web.cs.dal.ca/~mhe/csci3110/
Students are responsible for checking announcements posted on the course web page for
assignment clarifications, course updates and other informati
Lecture 5, CSCI 3110
Solution 2: Sort the n numbers, and then we walk through the sorted array, comparing each element with
the one immediately to its right. If we find any element that is equal to the element immediately to its
right, then we have found
Lecture 1, CSCI 3110
Today I first handed out a course information sheet, and gave an introduction to this course.
RAM, Algorithm Analysis and Order Notation
At the beginning of this course, let us learn some basic stuff about algorithm analysis.
Pseudoco
Lecture 9, CSCI 3110
Some experimental studies showed that Karatsuba's algorithm outperforms the pencil-and-paper
algorithm when n exceeds a certain threshold between 320 and 640. Thus it is good for very large
integers, which are often used in math, stat
Lecture 10, CSCI 3110
To design a better solution, we use divide and conquer.
DIVIDE: We split P into two subsets PL and PR of approximately equal size. That is, |PL| = |P|/2, and |
PR| = |P|/2. This can be done by sorting points by x-coordinate, and spli
Lecture 17, CSCI 3110
So far we have shown that the above randomized algorithm uses O(n) expected time, which is efficient.
There are however some issues with this solution. First, this solution is not robust: If there is no majority
element, then it will
Lecture 4, CSCI 3110
Solution 2
To improve the running time, we observe that in the previous solution, we perform a lot of recomputation
in the innermost loop. For the example given before, to computer the sum of the subarray x[3.5], we do a
loop to get t
Lecture 2, CSCI 3110
Analysis of Insertion Sort
To analyze algorithms in RAM, we first need understand that the running time of an algorithm is
expressed as a function of its input size. This is because the running time of an algorithm normally grows
when
Lecture 15, CSCI 3110
Making Change
In this problem, we are given an amount in cents. We are to make change using a system of
denominations, using the smallest number of coins possible to make change.
Canadian Coinage System (including pennies) and Greedy
Lecture 14, CSCI 3110
Our previous algorithm computes the optimal cost of multiplying the given matrix chain. How do we
multiply the matrices optimally? This can be done using recursion.
Say, we wish to multiply MiMi+1.Mj optimally, if we know k, then we
Lecture 16, CSCI 3110
Finck analyzed this algorithm in 1841. He proved the following theorem: Euclid's algorithm uses O(lg v)
steps on input (u, v), where u > v > 0.
Proof. Consider replacing (u, v) by (u, r), where r = u mod v. There are three cases.
Cas
Lecture 12, CSCI 3110
Pseudocode of the greedy algorithm:
Activity_Selection(s, f, n)
sort both s and f by f[i] in increasing order
A cfw_1
k 1
for i 2 to n do
if s[i] >= f[k] then
A A cfw_i
k i
return A
The time complexity is O(n lg n).
Knapsack Problem
Lecture 13, CSCI 3110
Dynamic Programming
Dynamic programming was popularized by Richard Bellman, who started to describe it in 1940s. It
typically applies to optimization problems. It typically reduces the complexity from exponential, e.g. 2n to
polynomi
Lecture 7, CSCI 3110
Recursion
Recursion is the second algorithm design paradigm that we discuss in this course. We all learned how to
do recursion in an introductory programming course, but now let's learn some issues regarding recursion,
as well as how
Lecture 6, CSCI 3110
Jarvis's March (Gift Wrapping): A better algorithm of computing convex hull is Jarvis' march. It is also
called gift wrapping as its computational process is similar to the process of wrapping a gift: start from an
extreme point and w
Lecture 11, CSCI 3110
Supporting Subrange Sum Under Updates
Now let's invent a data structure to solve the following problem: We would like to maintain an array
A[1.n] to support
Query a: Set A[i] A[i] +
Query b: Subrange sum (c, d): Computing A[c] + A[c
Lecture 19, CSCI 3110
We also claim that the resulting graph must be connected, for if it weren't, when we considered the edge
that connected two components, we would have added it, but we didn't.
Now we prove that the spanning tree, T, produced by Kruska
Lecture 3, CSCI 3110
Following this definition, we can determine that n, n2, 3n2 + 4n + 5 are all O(n2), while n3 is not.
When we say that the running time of insertion sort is O(n2), we mean that the worst-case running time of
insertion sort is O(n2). Wh
Lecture 8, CSCI 3110
Divide-and-Conquer
This is the 3rd algorithm design paradigm. We learned divide-and-conquer in previous courses, and we
will learn more about it in this course. The general steps of divide-and-conquer are
DIVIDE the given problem int
ure 18, CSCI 3110
Graph Algorithms
In previous lectures, we learned 8 algorithm design paradigms, which gives us a rich toolset for problem
solving. Now we use some of these paradigms to solve problems in graphs.
We learned graphs in CSCI 2110 before, and
Loading a Project
First download the IslandDemo5 zip file to the Desktop from here:
o web.cs.dal.ca/~sbrooks/csci4168/lectures/files/IslandDemo5.zip
o Then unnzip it on the desktop:
Startup Unity 3D. You can find it inside Start->Programs->Unity on Window
Importing the Outpost.unitypackage
Unity textbook comes with some models,
including an outpost building
Can also download here:
o /csci4168/lectures/files/outpost.zip
Easy to import:
1. Go to Assets > Import Package > Custom Package
2. Leave all files sel
Setting up a 3D World:
Well be looking at several things in this Unity tutorial:
o creating a terrain
o adding textures
o painting textures
o placing plants and trees
o adding lights
o and adding sounds
Unity isnt really a modelling program, because mos