ProgramBy :- Eman El-ma’asarawi2/24/20111
What is Program?How to make program?How to think about the program?Steps for making a good project.2/24/20112
2/24/20113
DSAlgorithmProgram+ =2/24/20114
 list of instruction to achieve specific task.ProgramI/P instructionProcessing instructionO/P instruction2/24/20115
Programming ToolboxesH/WS/WOsCompiler Text editor Idea wareAlgorithmDSProgramming MethodologiesStructure DesignOop S/W ConceptsInformation hidingData encapsulationData abstraction2/24/20116
AlgorithmDSMethodologiesApplication (text editor)Compiler OSHW2/24/20117
        DS (Data Structure)Data :- factual information Structure :- Arrangement or relationship of elements as particlesData structure :-A means of storing a collection of data2/24/20118
DS cont…A data structure can be viewed as consisting of a set of algorithms for performing operations on the data it stores.2/24/20119
Variable:-			data storage location that has a value that can change during program execution. Constant:-		fixed value that can’t change.2/24/201110
Applications of Data StructureDSLinearNon-LinearSequentialLinkedLinkedqueuesGraphsTreeLinkedstackslinkedlistsqueuearraystack2/24/201111
1-Array2-Linked List3-Stack4-Queue5-Tree2/24/201112
1-Array Is a data structure where all elements are stored in contiguous memoryn210Individual elementsArray 2/24/201113
Random accessFixed sizeLess efficient used on data stored in arrayArray propertiesArray sizeElement size1-in bytes2-the number of elementData type of array2/24/201114
Exambel01234567  1      2     34     56      2     78      0     102/24/201115
2-linked listIs a data structure in which the first element is  (head) stored on its ownDataList head /Dummy NodeNull2/24/201116
Linked list propertiesThe size not fixed grow/shrinkThe extra space is needed to store the pointer of each element More timeImplement linked list use array but not efficientComplex to code and manage2/24/201117
Appending Node		add Node to the end of the list.Traversing		check the linked list.Inserting Node		add Node in the middle of a list.Deleting Node from memory			 Modified linksDestroyOperation on linked list2/24/201118
Which is fast array or linked list?Why?2/24/201119
Is a data structure consisting of data nodes connected to each other with pointers.Each node connected to 2 or more other nodes.2/24/201120			5-Trees
The order of the tree:-		is the max number of nodes to which any single node connected. binary tree:-		is the simplest tree is of order 2.	each child have two pointers.Root node:-		the topmost node in the tree. leaf node:-	node with no children.2/24/201121Tree properties
DatarightleftDatarightleftDatarightleft2/24/201122
Create binary treeInserting NodeTraversing the tree recursionSearching the treeDeleting the tree2/24/201123Binary search tree op…
Logical sequence of steps describe complete solution to solve problem in finite amount of timeMay involve alternation, iteration or recursionMore than one algorithm possible for the same taskAlgorithms2/24/201124
Sorting 				Searching-Insertion sort 			-linear search-Merge sort			-Binary search	-Heap sort-Quick sort2/24/201125Algorithmes
What resources are required to accomplish the task How one algorithm compares with other algorithms How much time or space is required Measured in terms of common basic operationsEfficiency of Algorithms2/24/201126
Worst-case: (usually)           T(n) = maximum time of algorithm on any input of size n.Average-case: (sometimes)T(n) = expected time of algorithm over all inputs of size n.Need assumption of statistical distribution of inputs.Best-case: (bogus)             Cheat with a slow algorithm that works fast on some input.2/24/201127Kinds of analyses
Running time: On a particular input, it is the number ofprimitive operations (steps) executed. One line may take a different amount of time thananother, but each execution of line i takes the sameamount of time c .Running time of the algorithm is	Ʃ (cost of statement)*(number of times statement is executed)2/24/201128
How efficiency varies with the size of the task E.g. if the size of the task increases linearly, do the efficiency measures increase linearly, quadratically, exponentially,...? Expressed in terms of standard functions of nComplexity2/24/201129
notationsBig oBig ƟBig Ω	F(n)=g(n)C1g(n)<=f(n)<=c2g(n)F(n)<=g(n)F(n)>=g(n)Data shapeEx - insertion sortbest caseData methodEx – insertion sortworst case2/24/201130
PseudoCodeconventionsIndentation indicates block structure. While, for, repeat , if , then, and else.▹indicates comment.indicates assignment.Variables are local by default.Array elements accessed as in A[i].2/24/201131
Calculate x^n, where n is an integer greater than 0.Algorithm1. Set r = 12. Set i = 13. Repeat	3.1 If i=n then terminate loop	3.2 Multiply r by x	3.3 Add 1 to i4. Exit with result rExampleSimple Power Algorithm2/24/201132
O(n) - the algorithm requires n multiplications.Time complexity2/24/201133
Formulate a Concise Specification of the ProblemDesign a high-level strategy for a solutionRefine steps 1 and 2 if necessaryComplete the details of the designImplement in the chosen languageHow to Approach Recursive Algorithms2/24/201134
Calculate x^n, where n is an integer greater than 0. Recursive Algorithm1. If n=0 then 1.1 set r = 1 2. Else 2.1 set p = n/2 (dropping any remainder) 2.2 set r = (xp)2 2.3 if n is odd then 2.3.1 multiply r by x 3. Exit with result rRecursive Power Algorithm2/24/201135
static int power (int x, int n) {	int r;	if (n == 0)		r = 1;	else {		r = power(x,n/2);		r = r*r;		if (n%2 != 0)			r *= x;	}	return r;}2/24/201136implementation
O(log n) - the algorithm requires approximately log   n multiplications.2Time complexity2/24/201137
2/24/201138
QuizTowers of Hanoi1232/24/201139
1232/24/201140
1232/24/201141
1232/24/201142
1232/24/201143
1232/24/201144
1232/24/201145
1232/24/201146
Solution' ≡ shortest path Recursive Solution:1. Identify biggest discrepancy (=disk N) 2. If moveable to goal peg Then move      Else3.   Subgoal: set-up (N−1)-disk tower on non-goal peg. 4. Go to 1.  ...2/24/201147
2/24/201148
SDLC (s/w Development life cycle)DesignProblem analysisMaintenance TestingRequirement definitionsImplementation Using program Delivery2/24/201149
The End2/24/201150

Algorithms

  • 1.
    ProgramBy :- EmanEl-ma’asarawi2/24/20111
  • 2.
    What is Program?Howto make program?How to think about the program?Steps for making a good project.2/24/20112
  • 3.
  • 4.
  • 5.
    list ofinstruction to achieve specific task.ProgramI/P instructionProcessing instructionO/P instruction2/24/20115
  • 6.
    Programming ToolboxesH/WS/WOsCompiler Texteditor Idea wareAlgorithmDSProgramming MethodologiesStructure DesignOop S/W ConceptsInformation hidingData encapsulationData abstraction2/24/20116
  • 7.
  • 8.
    DS (Data Structure)Data :- factual information Structure :- Arrangement or relationship of elements as particlesData structure :-A means of storing a collection of data2/24/20118
  • 9.
    DS cont…A datastructure can be viewed as consisting of a set of algorithms for performing operations on the data it stores.2/24/20119
  • 10.
    Variable:- data storage locationthat has a value that can change during program execution. Constant:- fixed value that can’t change.2/24/201110
  • 11.
    Applications of DataStructureDSLinearNon-LinearSequentialLinkedLinkedqueuesGraphsTreeLinkedstackslinkedlistsqueuearraystack2/24/201111
  • 12.
  • 13.
    1-Array Is adata structure where all elements are stored in contiguous memoryn210Individual elementsArray 2/24/201113
  • 14.
    Random accessFixed sizeLessefficient used on data stored in arrayArray propertiesArray sizeElement size1-in bytes2-the number of elementData type of array2/24/201114
  • 15.
    Exambel01234567 1 2 34 56 2 78 0 102/24/201115
  • 16.
    2-linked listIs adata structure in which the first element is (head) stored on its ownDataList head /Dummy NodeNull2/24/201116
  • 17.
    Linked list propertiesThesize not fixed grow/shrinkThe extra space is needed to store the pointer of each element More timeImplement linked list use array but not efficientComplex to code and manage2/24/201117
  • 18.
    Appending Node add Nodeto the end of the list.Traversing check the linked list.Inserting Node add Node in the middle of a list.Deleting Node from memory Modified linksDestroyOperation on linked list2/24/201118
  • 19.
    Which is fastarray or linked list?Why?2/24/201119
  • 20.
    Is a datastructure consisting of data nodes connected to each other with pointers.Each node connected to 2 or more other nodes.2/24/201120 5-Trees
  • 21.
    The order ofthe tree:- is the max number of nodes to which any single node connected. binary tree:- is the simplest tree is of order 2. each child have two pointers.Root node:- the topmost node in the tree. leaf node:- node with no children.2/24/201121Tree properties
  • 22.
  • 23.
    Create binary treeInsertingNodeTraversing the tree recursionSearching the treeDeleting the tree2/24/201123Binary search tree op…
  • 24.
    Logical sequence ofsteps describe complete solution to solve problem in finite amount of timeMay involve alternation, iteration or recursionMore than one algorithm possible for the same taskAlgorithms2/24/201124
  • 25.
    Sorting Searching-Insertion sort -linear search-Merge sort -Binary search -Heap sort-Quick sort2/24/201125Algorithmes
  • 26.
    What resources arerequired to accomplish the task How one algorithm compares with other algorithms How much time or space is required Measured in terms of common basic operationsEfficiency of Algorithms2/24/201126
  • 27.
    Worst-case: (usually) T(n) = maximum time of algorithm on any input of size n.Average-case: (sometimes)T(n) = expected time of algorithm over all inputs of size n.Need assumption of statistical distribution of inputs.Best-case: (bogus) Cheat with a slow algorithm that works fast on some input.2/24/201127Kinds of analyses
  • 28.
    Running time: Ona particular input, it is the number ofprimitive operations (steps) executed. One line may take a different amount of time thananother, but each execution of line i takes the sameamount of time c .Running time of the algorithm is Ʃ (cost of statement)*(number of times statement is executed)2/24/201128
  • 29.
    How efficiency varieswith the size of the task E.g. if the size of the task increases linearly, do the efficiency measures increase linearly, quadratically, exponentially,...? Expressed in terms of standard functions of nComplexity2/24/201129
  • 30.
    notationsBig oBig ƟBigΩ F(n)=g(n)C1g(n)<=f(n)<=c2g(n)F(n)<=g(n)F(n)>=g(n)Data shapeEx - insertion sortbest caseData methodEx – insertion sortworst case2/24/201130
  • 31.
    PseudoCodeconventionsIndentation indicates blockstructure. While, for, repeat , if , then, and else.▹indicates comment.indicates assignment.Variables are local by default.Array elements accessed as in A[i].2/24/201131
  • 32.
    Calculate x^n, wheren is an integer greater than 0.Algorithm1. Set r = 12. Set i = 13. Repeat 3.1 If i=n then terminate loop 3.2 Multiply r by x 3.3 Add 1 to i4. Exit with result rExampleSimple Power Algorithm2/24/201132
  • 33.
    O(n) - thealgorithm requires n multiplications.Time complexity2/24/201133
  • 34.
    Formulate a ConciseSpecification of the ProblemDesign a high-level strategy for a solutionRefine steps 1 and 2 if necessaryComplete the details of the designImplement in the chosen languageHow to Approach Recursive Algorithms2/24/201134
  • 35.
    Calculate x^n, wheren is an integer greater than 0. Recursive Algorithm1. If n=0 then 1.1 set r = 1 2. Else 2.1 set p = n/2 (dropping any remainder) 2.2 set r = (xp)2 2.3 if n is odd then 2.3.1 multiply r by x 3. Exit with result rRecursive Power Algorithm2/24/201135
  • 36.
    static int power(int x, int n) { int r; if (n == 0) r = 1; else { r = power(x,n/2); r = r*r; if (n%2 != 0) r *= x; } return r;}2/24/201136implementation
  • 37.
    O(log n) -the algorithm requires approximately log n multiplications.2Time complexity2/24/201137
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
    Solution' ≡ shortestpath Recursive Solution:1. Identify biggest discrepancy (=disk N) 2. If moveable to goal peg Then move Else3. Subgoal: set-up (N−1)-disk tower on non-goal peg. 4. Go to 1. ...2/24/201147
  • 48.
  • 49.
    SDLC (s/w Developmentlife cycle)DesignProblem analysisMaintenance TestingRequirement definitionsImplementation Using program Delivery2/24/201149
  • 50.