Data Structures and Algorithms
Complexity
PLSD210
A General Portable Performance Metric
Informally, Time to solve a problem of size, n,
T(n) is O(logn)
T(n) = c log2n
Formally:
O( g(n) ) is the set of functions, f, such that
f(n) < c g(n)
ie for suffic
Counting Sort
Counting Sort
Principle: Find the the position of an element.
Counting Sort
Principle: Find the the position of an element.
Input
5
3
6
1
5
7
1
9
5
2
3
0
1
2
3
4
5
6
7
8
9
10
Counting Sort
Principle: Find the the position of an element.
Inpu
Searching
And
Sorting
Linear Search
Read an array of elements from the user
Read a key to be searched
Search the key in the array and print its position
if found
Linear Search
printf("Enter the number to search: ");
#include <stdio.h>
scanf("0", &key);
vo
Functions
What is a function?
A number of statements grouped in a single logical
unit is referred to as a function.
The function main() in the program is executed first.
When are the other functions executed?
When they are called directly or indirectly fr
Control Structures
Topics
Need to perform repeated actions or skip some statements
Use control statements which alter the flow of execution of the
program
Two types:
Decision Making
if statement
if-else statement
switch statement
Loop constructs
for-loop
Linked Lists
- Delete(START)
start
a
z
Instructions
b
c
y
d
e
f
Linked Lists
- Delete(START)
start
a
z
b
c
y
Instructions
start = start.pointer;
d
e
f
Linked Lists
- Delete(START)
start
a
z
Instructions
b
c
y
d
e
f
Linked Lists
- Delete(START)
start
a
b
c
Data Structures
Data Structures
How to organize data
Data Structures
How to organize data
Store data
Data Structures
How to organize data
Store data
Retrieve data
Data Structures
- Array
0
1
2
3
4
5
a
6
7
8
9
10
Data Structures
- Array
0
1
2
3
4
5
a
a[3]
Linked Lists Delete at Start
start
a
b
c
d
e
Linked Lists Delete at Start
start
a
deleteStart(
b
c
d
) cfw_
e
Linked Lists Delete at Start
start
a
b
/* deleteStart(&start) */
deleteStart(
c
d
) cfw_
e
Linked Lists Delete at Start
start
a
b
c
/* deleteSt
Arrays
Arrays
1
Why Arrays?
Read ages for
Arrays
5 people, print the respective ages and their
average
int a1, a2, a3, a4, a5;
Read values for a1 to a5 . scanf 5 times
Print values for a1 to a5 . printf 5 times
2
Why Arrays?
Read ages for
5 people, print
Introduction to the
C Programming Language
Introduction to C
1
Software Life Cycle
Problem statement
Problem analysis and design
Analyse the method to solve the problem. Develop
an algorithm. Represent it as flowchart or
pseudocode
Analysis of the algorit
Data Structures and Algorithms
Graphs
Single-Source Shortest Paths
(Dijkstras Algorithm)
PLSD210(ii)
Graphs - Shortest Paths
Application
In a graph in which edges have costs .
Find the shortest path from a source to a destination
Surprisingly .
While
Data Structures and Algorithms
Games
PLSD210
PLSD210
Lecture 22 - Key points
Hard Problems
Class P - O(nk) algorithm problems
Class NP - non-deterministic polynomial
Guessing step
Verify solution in polynomial time
Problems are believed to lie in NP
Data Structures and Algorithms
Multiplying Polynomials
and the
Fast Fourier Transform
PLSD210(ii)
Polynomial Multiplication
Given two polynomials
n
f = ai xi
i=0
n
g = bi xi
i=0
We can generate a product in O(n2) time
Each of the 2n coefficients of the
Data Structures and Algorithms
Graphs
Graph Representations
PLSD210(ii)
Graphs - Data Structures
Vertices
Map to consecutive integers
Store vertices in an array
Edges
Adjacency Matrix
Booleans TRUE - edge exists
FALSE - no edge
O(|V|2) space
Graphs
Data Structures and Algorithms
Huffman Encoding and Decoding
PLSD210(ii)
Huffman Encoding
Compression
Typically, in files and messages,
Each character requires 1 byte or 8 bits
Already wasting 1 bit for most purposes!
Question
Whats the smallest num
Data Structures and Algorithms
Graphs
Minimum Spanning Tree
PLSD210
Key Points - Lecture 19
Dynamic Algorithms
Optimal Binary Search Tree
Used when
some items are requested more often than others
frequency for each item is known
Minimises cost of all
Data Structures and Algorithms
PLSD210
Key Points
Lectures 1 & 2
Data Structures and Algorithms
The key to your professional reputation
A much more dramatic effect can be made on
the performance of a program by changing to a
better algorithm than by
h
Data Structures and Algorithms
Searching
Hash Tables
PLSD210
Hash Tables
All search structures so far
Relied on a comparison operation
Performance O(n) or O( log n)
Assume I have a function
f ( key ) integer
ie one that maps a key to an integer
What
Data Structures and Algorithms
PLSD210
Sorting
Sorting
Card players all know how to sort
First card is already sorted
With all the rest,
Scan back from the end until you find the first card larger
than the new one,
Move all the lower ones up one slot
i
xv6
a simple, Unixlike teaching operating system
Russ Cox
Frans Kaashoek
Robert Morris
Draft as of August 28, 2012
Contents
0
Operating system interfaces
1
The rst process
17
2
Page tables
25
3
Traps, interrupts, and drivers
33
4
Locking
45
5
Scheduling
5
Data Structures
- Queues
Data Structures
- Queues
front
a
b
c
d
rear
Data Structures
- Queues
front
a
Store data
b
c
d
push()
rear
Data Structures
- Queues
front
a
b
Retrieve Data
c
d
pop()
rear
Queues
- Implementation
Queues
- Implementation
Queue
q=
0
1
8/4/2016
algorithmDeterminingthebigOruntimesofthesedifferentloops?StackOverflow
signup
login
tour
help
Dismiss
AnnouncingStackOverflowDocumentation
WestartedwithQ&A.Technicaldocumentationisnext,andweneedyourhelp.
Whetheryou'reabeginneroranexperienceddevel
Question 1:
The correct output of the following C-program is 32. However, some of the operators
in the computation to calculate 'res' in the program are incorrect . Hence, correct
the computation with appropriate operators so that 'res' stores the correct
Students sitting in odd numbered cubicle will attempt section A. Students sitting in
even numbered cubicle will attempt section B. Every one will attempt section C.
Anybody not abiding by this instruction will be awarded 0 marks irrespective of his / her
CS101Introductiontocomputing
ProblemSolving
(Computing)
A.Sahu andS.V.Rao
Dept of Comp. Sc. & Engg.
DeptofComp.Sc.&Engg.
IndianInstituteofTechnologyGuwahati
1
Outline
Loopinvariantandlooptermination
ManyProblemSolvingExamples
Man Problem Sol ing E ample
CS101Introductiontocomputing
Recursion
and
ProblemSolving
A.Sahu andS.V.Rao
Dept of Comp. Sc. & Engg.
DeptofComp.Sc.&Engg.
IndianInstituteofTechnologyGuwahati
Outline
RecursiveFunction(Continued)
Example
l
ProblemSolvingExample
Problem Solving Example
U