CS4710 Algorithms. What is an Algorithm? An algorithm is a procedure to perform some task. 1.General - applicable in a variety of situations 2.Step-by-step.

Slides:



Advertisements
Similar presentations
Algorithm Design Techniques
Advertisements

Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU.
Fibonacci Numbers F n = F n-1 + F n-2 F 0 =0, F 1 =1 – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … Straightforward recursive procedure is slow! Why? How slow? Lets.
Lecture 15. Graph Algorithms
O(N 1.5 ) divide-and-conquer technique for Minimum Spanning Tree problem Step 1: Divide the graph into  N sub-graph by clustering. Step 2: Solve each.
CS252: Systems Programming Ninghui Li Program Interview Questions.
Types of Algorithms.
DATA STRUCTURES & ANALYSIS OF ALGORITHMS
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Recursion. Binary search example postponed to end of lecture.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Transforming Infix to Postfix
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
The Design and Analysis of Algorithms
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Instructor: Dr. Sahar Shabanah Fall Lectures ST, 9:30 pm-11:00 pm Text book: M. T. Goodrich and R. Tamassia, “Data Structures and Algorithms in.
Algorithms and Data Structures Lecture X
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 13. Abstract Data Types (ADT’s)
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
PROBLEM-SOLVING TECHNIQUES Rocky K. C. Chang November 10, 2015.
Graphs Upon completion you will be able to:
CS 146: Data Structures and Algorithms July 28 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Final Exam Review CS Total Points – 20 Points Writing Programs – 65 Points Tracing Algorithms, determining results, and drawing pictures – 50.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
Data Structures and Algorithms
Data Structure Interview Question and Answers
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Basic Data Structures.
Datastructure.
Data Structures and Algorithms
structures and their relationships." - Linus Torvalds
Chapter 1.
Lecture 5 Dynamic Programming
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Introduction to Data Structures
Graphs.
DATA STRUCTURE.
structures and their relationships." - Linus Torvalds
Data Structures and Algorithms
Presentation transcript:

CS4710 Algorithms

What is an Algorithm? An algorithm is a procedure to perform some task. 1.General - applicable in a variety of situations 2.Step-by-step - each step must be clear and concise 3.Finite - must perform task in a finite amount of time 4.Note: Is not the same as an actual implementation

Common algorithmic categories Recursion Divide and conquer Dynamic programming Greedy

Recursion Definition:  An algorithm defined in terms of itself is said to use recursion or be recursive. Examples  Factorial 1! = 1 n! = n x (n-1)!, n > 1  Fibonacci sequence f(1) = 1 f(2) = 1 f(n) = f(n-1) + f(n-2)

Recursion Recursion is natural for some problems  Many solutions can be expressed easily using recursion  Lead often to very elegant solutions  Extremely useful when processing a data structure that is recursive Warning!  Can be very costly when implemented!  Calling a function entails overhead  Overhead can be high when function calls are numerous (stack overflow)  Some software is smart enough to optimize recursive code into equivalent iterative (low overhead) code

Recursive factorial algorithm (written in pseudo code) factorial(n) if n=1 return 1 else return n * factorial(n-1)

Recursive factorial code # a recursive factorial routine in Python def fact(n): if n == 1: return 1 else: return n * fact(n - 1)

Recursive factorial code # a recursive factorial routine in Perl sub factorial_recursive { my ($n) = shift; return 1 if $n = 1; return $n * factorial_recursive($n – 1); }

Non-recursive factorial code # an iterative factorial routine in Perl sub factorial_iterative { my ($n) = shift; my ($answer, $i) = (1, 2); for ( ; $i <= $n; $i++) { $answer *= $i; } return $answer); }

Divide and conquer Secrets of this technique:  Top-down technique  Divide the problem into independent smaller problems  Solve smaller problems  Combine smaller results into larger result thereby “conquering” the original problem. Examples  Mergesort  Quicksort

Dynamic programming Qualities of this technique:  Useful when dividing the problem into parts creates interdependent sub-problems  Bottom-up approach  Cache intermediate results  Prevents recalculation  May employ “memo-izing”  May “dynamically” figure out how calculation will proceed based on the data Examples  Matrix chain product  Floyd’s all-pairs shortest paths problem

Matrix chain product Want to multiply matrices A x B x C x D x E We could parenthesize many ways 1. (A x (B x (C x (D x E)))) 2. ((((A x B) x C) x D) x E) 3. … Each different way presents different number of multiplies! How can we decide on a wise approach?

Dynamic programming applied to matrix chain product Original matrix chain product A x B x C x D x E (ABCDE for short) Calculate in advance the cost (multiplies) AB, BC, CD, DE Use those to find the cheapest way to form ABC, BCD, CDE From that derive best way to form ABCDE

Greedy algorithms Qualities of this technique:  Naïve approach  Little to no look ahead  Fast to code and run  Often gives sub-optimal results  For some problems, may be optimal Examples where optimal  MST of a graph

Data structures do matter Although algorithms are meant to be general, 1.One must choose wisely the representation for data, and/or 2.Pay close attention to the data structure already employed, and/or 3.Occasionally transfer the data to another data structure for better processing. Algorithms and data structures go hand in hand 1.The steps of your algorithm… 2.What your chosen data structure allows easily…

Some of python’s built-in types and data structures type int - integers type float - floating point numbers type str - Strings or text python sequence or list - (similar to an array in other languages, but more powerful than a true array) python dictionary - basically a hash table where each data value has an associated key used for lookup.

Some of Perl’s built-in data structures $scalar  number (integer or float)  string (sequence of characters)  reference (“pointer” to another data structure)  object (data structure that is created from a (a sequence of scalars indexed by integers) %hash (collection of scalars selected by strings (keys))

Perl (and python) arrays are powerful! Can dynamically grow and shrink (not normal array behavior) Need a queue? (FIFO)  Can use an array  Add data with push operator (enqueue)  Remove using shift operator (dequeue) Need a stack? (LIFO)  Can use an array  Push data with push operator (push)  Pop data using pop operator (pop)

Advanced data structures Linked lists Circular linked lists Doubly linked lists Binary search trees Graphs Heaps Binary heaps Hash tables

Linked list Consists of small node structures Each node oStores one data item (data field) oStores a reference to next node (next field) Allows non-contiguous storage of data Is much like a magazine article

Linked list Benefits oCan insert more data (more nodes) in middle of list efficiently oCan remove data from middle efficiently Word processors typically store text using linked lists Allows for very fast cutting and pasting. Cons oTakes up more space (for the references) oTrue array would take up less space

Graphs Many representations  Adjacency lists  Adjacency matrix Must consider representation when processing Some graphs are weighted, others not Some are directed, others have implied bi- direction

Graphs Graph Searches  Depth-first search Uses stack Yields a path Exhaustive  Breadth-first search Uses a queue Yields a shortest path Exhaustive

Graphs Greedy algorithms for graphs  Minimum spanning tree (MST) Prim’s algorithm Grow an MST from a single node Optimal solution Kruskal’s algorithm Keep picking cheap edges Optional solution