CSC 2720: Data Structures
Shiraj Pokharel
Attendance (Please use GSU WiFi):
https://ousp.cs.gsu.edu/seat/
GSU :
" Every Instructor with in-person classes should
prepare a seating chart during the first class session.
This will enable contact tracers to identify who is in
“close contact” if a positive COVID-19 test is
reported. For this same reason, a daily attendance
sheet is recommended."
Classroom Values
●Respect
respect your professor, each other, and yourself
●Bravery
ask yourself “what is the worst that could happen?”
●Vulnerability
let me help you in whatever ways I can
●Grit
failure does not define you
The Medium
In a fine art context, “art medium” refers to the art
materials or art supplies used to create a work of
art. Basically, it is whatever you use to mark the
surface
The Medium: In Computer Science
Coding is actually just the medium in which we do
computer science.
Thus, thinking about the problems that we bring up
in class are ways to do computer science (even
without a computer or coding)
The Medium: In Computer Science
This might be your first computer science class ever.
Coding is actually just the medium in which we do
computer science.
Data structures and algorithms are important
elements of computer science in which we learn
how to think.
Tentative Schedule
• Unit 1: Prerequisite Review & Basic Algorithms
• Unit 2: Asymptotic Analysis
• Unit 3: Sorting
• Unit 4: Data Structures
• Unit 5: Advanced Topics
Course Components
• Ref : Course Syllabus
Getting Help
The staff will have ample office hours as noted on the
syllabus. Please use them
Lecture 1 and 2 :
Divide-and-Conquer (Part 1)
Lecture 1 Objectives
- Explain and analyze the optimal algorithm for
solving the "Guess My Number" game.
- Think like a computer scientist. Start with the slow strategy and then
get better and analyze the improvements.
- Understand logarithms and explore how they help
us analyze algorithms.
Lecture 1 Agenda
• Guessing a number between 0 and 31
• Guessing a number between 0 and 2 -1
n
• Logarithms
• Approximating a formula for number of guesses
G(n)
• An exact formula for number of guesses G(n)
Lecture 1 Agenda
• Guessing a number between 0 and 31
• Guessing a number between 0 and 2 -1
n
• Logarithms
• Approximating a formula for number of guesses
G(n)
• An exact formula for number of guesses G(n)
Guess My Number
I am thinking of a number between 0 and 31, inclusive.
Guess what it is! After each guess, I will tell you if your
number is correct, too low, or too high.
1) What are the lowest and highest number of guesses
possible?
2) What is the best strategy?
3) In the best strategy, what is the worst case?
Guess My Number
I am thinking of a number between 0 and 31, inclusive.
Guess what it is! After each guess, I will tell you if your
number is correct, too low, or too high.
1) What are the lowest and highest number of guesses
possible? Lowest: 1, Highest: 32
2) What is the best strategy?
3) In the best strategy, what is the worst case?
Guess My Number
I am thinking of a number between 0 and 31, inclusive.
Guess what it is! After each guess, I will tell you if your
number is correct, too low, or too high.
1) What are the lowest and highest number of guesses
possible? Lowest: 1, Highest: 32
2) What is the best strategy? Guess middle each time
3) In the best strategy, what is the worst case?
Guess My Number
I am thinking of a number between 0 and 31, inclusive.
Guess what it is! After each guess, I will tell you if your
number is correct, too low, or too high.
1) What are the lowest and highest number of guesses
possible? Lowest: 1, Highest: 32
2) What is the best strategy? Guess middle each time
3) In the best strategy, what is the worst case? 6
Guessing a number between 0 and
31
It turns out that the optimal strategy of guessing the
middle every time takes 6 guesses in the worst case.
Try doing this example for your exercise
Lecture 1 Agenda
• Guessing a number between 0 and 31
• Guessing a number between 0 and 2 -1k
• Logarithms
• Approximating a formula for number of guesses
G(n)
• An exact formula for number of guesses G(n)
Guessing a number between 0 and 2 k
-1
Fill in the below table of # of guesses G(n) as a
function of n (recall that we are guessing a number
between 0 and n-1)
n 1 2 4 8 16 32
G(n) 6
Guessing a number between 0 and
2 -1
k
Fill in the below table of # of guesses G(n) as a
function of n (recall that we are guessing a number
between 0 and n-1)
n 1 2 4 8 16 32
G(n) 1 2 3 4 5 6
Lecture 1 Agenda
• Guessing a number between 0 and 31
• Guessing a number between 0 and 2 -1
n
• Logarithms
• Approximating a formula for number of guesses
G(n)
• An exact formula for number of guesses G(n)
Logarithms
What mathematical function fits the pattern below?
n 1 2 4 8 16 32
G(n) 1 2 3 4 5 6
Logarithms
What mathematical function fits the pattern below?
Recall that y = log2(n) is the solution to 2 = n.
y
n 1 2 4 8 16 32
G(n) 1 2 3 4 5 6
Logarithms
Here are some useful properties of logs:
• log(ab) = log(a) + log(b)
• log(a ) = b log(a)
b
• logb(a) = log(a) / log(b)
Important: Normally if you see log(n) it refers to log10 (n)
However, in this class it will refer to log2(n)
Lecture 1 Agenda
• Guessing a number between 0 and 31
• Guessing a number between 0 and 2 -1
n
• Logarithms
• Approximating a formula for number of guesses
G(n)
• An exact formula for number of guesses G(n)
Approximating a formula for G(n)
Recall that y = log2(x) is the solution to 2 = x.
y
n 1 2 4 8 16 32
log2(n)
G(n) 1 2 3 4 5 6
Approximating a formula for G(n)
Recall that y = log2(x) is the solution to 2 = x.
y
n 1 2 4 8 16 32
log2(n) 0 1 2 3 4 5
G(n) 1 2 3 4 5 6
Approximating a formula for G(n)
Recall that y = log2(x) is the solution to 2 = x.
y
Given this data, we see that for n a power of 2, we
have G(n) = log2(n) + 1.
n 1 2 4 8 16 32
log2(n) 0 1 2 3 4 5
G(n) 1 2 3 4 5 6
Lecture 1 Agenda
• Guessing a number between 0 and 31
• Guessing a number between 0 and 2 -1
n
• Logarithms
• Approximating a formula for number of guesses
G(n)
• An exact formula for number of guesses G(n)
An exact formula for number of
guesses G(n)
Fill in the below table of # of guesses G(n) as a function of n (recall
that we are guessing a number between 0 and n-1), and then fill in the
formula:
G(n) = ???
n 4 5 6 7 8
G(n) 3 4
An exact formula for number of
guesses G(n)
Fill in the below table of # of guesses G(n) as a function of n (recall
that we are guessing a number between 0 and n-1), and then fill in the
formula:
G(n) = Floor(log(n)) + 1
n 1 2 4 8 16 32
G(n) 6
An exact formula for number of
guesses G(n)
Fill in the below table of # of guesses G(n) as a function of n (recall
that we are guessing a number between 0 and n-1), and then fill in the
formula:
G(n) = Floor(log(n)) + 1
n 4 5 6 7 8
G(n) 3 3 3 3 4
Divide and Conquer
This algorithm is an example of a class of algorithms called divide and
conquer.
• Divide your problem space, and then conquer the relevant part.
• Rinse and repeat
Lecture 2: Divide-and-Conquer
(Part 2)
Review
Remember from last class, we came up with an algorithm for the "Guess My
Number Game".
It roughly looked like this:
Given range 0-x inclusive:
1) Pick the middle of the range. (0+x)/2, rounded down if necessary. If you're
right, celebrate!
2) If the answer was too low, repeat mid+1 being the new start
3) If answer was too high, repeat with mid-1 being the new end
Review
If our game was between 0 and 15, let’s walk through what that process looks
like one more time.
Review: Arrays
Consider array x which is [0, 2, 5, 7]
In small groups, figure out what these would output:
1) x[0]
2) x[2]
3) x[4]
4) x.length
Review: Arrays
Consider array x which is [0, 2, 5, 7]
In small groups, figure out what these would output:
1) x[0] 0
2) x[2]
3) x[4]
4) x.length
Review: Arrays
Consider array x which is [0, 2, 5, 7]
In small groups, figure out what these would output:
1) x[0] 0
2) x[2] 5
3) x[4]
4) x.length
Review: Arrays
Consider array x which is [0, 2, 5, 7]
In small groups, figure out what these would output:
1) x[0] 0
2) x[2] 5
3) x[4] IndexOutOfBoundsException / Index Error
4) x.length
Review: Arrays
Consider array x which is [0, 2, 5, 7]
In small groups, figure out what these would output:
1) x[0] 0
2) x[2] 5
3) x[4] IndexOutOfBoundsException / Index Error
4) x.length 4
Lecture 2 Objectives
After today, you will know how to code the optimal
algorithm for solving the "Guess My Number" game.
Putting the solution into code
1. Start the game with some print statements and
input
2. Initialize guess
3. Loop until you get it right (each time you get it
wrong, adjust your guess)
Putting the solution into code
• If you don’t already have your IDE/Coding
Environment ready Go to repl.it and select “Python“
• Write up the code for the guessing function we
“solved” together and verify that your simple
guessing solution works
Review
Remember, we came up with a solutioning process AKA “algorithm” for the
"Guess My Number Game".
It roughly looked like this:
Given range 0-x inclusive:
1) Pick the middle of the range. (0+x)/2, rounded down if necessary. If you're
right, celebrate!
2) If the answer was too low, repeat mid+1 being the new start
3) If answer was too high, repeat with mid-1 being the new end
Optimize your solution
Now, it's time to take the algorithm and adjust it to be
the optimal . Do this on your own computer, but you
may talk to the people around you
Announcements:
Labs Start Today