Docsity

Introduction to Algorithms & Data Structures, Study notes of Computer Science

Partial preview of the text

Download Introduction to Algorithms & Data Structures and more Study notes Computer Science in PDF only on Docsity!

UNIT- I

Introduction to Algorithms & Data

Structures

Contents

Introduction

General Problem Solving Concepts

Types of Problems

Problem solving with computers

Difficulties with problem solving

Problem solving Aspect

Problem Solving Concepts for

Computer

Constants and variables

Data types

Functions

Operators

Expressions and equations

Programming Concepts

Communicating with computers

Organizing the problem

Using the tools

Testing the solution

Coding the program

Example: Problem what to do this evening

1. Indentify the Problem: How do the Individuals wish to spend the

evening?

2. Understand the problem: The knowledge base of the participants

must be considered. The only solutions that should be selected are

ones that everyone would know how to do. You probably would not

select as a possible solution playing a game of chess if the

participants do not know how to play chess.

3. Identify Alternatives :

▫

Watch Television

▫ Invite friends over

▫ Play games

▫ Go to the movie

▫ Play miniature golf

▫ Go to a friends party

▫ Go to the amusement park

▫ List is complete only when you can think of no more alternatives

4. Select best way to solve the problem

▫ Cut out alternatives that are not acceptable

▫ Specify pros and Cons of each Remaining alternative

▫ Compare pros and cons to make final decision

General Problem-Solving Concepts

Problem solving with

computers

Results means the outcome or the completed

computer assisted answer

Program means the set of instructions that make up

the solution using programming language

Computers are built to deal with algorithmic solutions

Difficulty lies in Programming

Solutions must be transformed into an algorithmic

format

Difficulties with Problem

Solving

People have many problems with problem solving

Afraid to make decisions

People go through one or more steps incorrectly

Problem solving process is not easy

Problem solving on computer

Difficult task of writing instructions

Computer has specific system of communication

Problem Definition Phase

  • What must be done?
  • We must try to extract set of precisely defined tasks.
  • E.g. Finding Square root, Greatest Common Divisor

Getting Started on a Problem

  • Sometimes, even after problem definition people do

not know where to start?

  • What can we do?

The use of Specific Examples

  • Best approach when we are stuck is to make a start on a

problem is to pick a specific example

  • Geometrical and graphical representing certain aspect of

a problem can be useful

Similarities among problems

  • Bring as much as past experiences as possible
  • New problem cannot be completely different
  • Sometimes it blocks us from discovering a new thing
  • In the first instance try to independently solve the problem

Algorithm

“Algorithm is any well defined

computational procedure that takes

some values or set of values as input

and produces some value or a set of

values as output”

Characteristics of algorithm

  • Input : algorithm is supplied with zero or more external

quantities

  • Output : Algorithm must produce one or more results/output
  • Definiteness : Each step in an algorithm must be clear and

unambiguous

  • Finiteness: Must have finite number of steps
  • Effectiveness : Every instruction must be sufficiently basic
  • Feasibility − Should be feasible with the available resources.
  • Independent − An algorithm should have step-by-step

directions, which should be independent of any programming

code.

Algorithms tell the programmers how to code

the program. Alternatively, the algorithm can be

written as −

Step 1 − START ADD

Step 2 − get values of a & b

Step 3 − c ← a + b

Step 4 − display c

Step 5 − STOP

Pseudo code

Expression:

Use standard mathematical symbols to

describe numeric and boolean expression

Use  for assignment

Use = for equality relationship

Method Declaration

Algorithm name( param1,param2)

Return : return value

Pseudo code

Algorithm arrayMax(A,n)

//Input: An array A storing n integers

//Output: The maximum element in A

currentMax A[0]

for i1 to n-1 do

If currentMax<A[i] then currentMaxA[i]

return currentMax

Pseudo code

Algorithm arraySum(A,n)

//Input: An array A storing n integers

//Output: The Addition of elements in A

sum:=

for i:=0 to n-1do

sum= sum + a [i]

return (sum)