44
Data Structure and Algorithms

1-Data Structure and Algorithms

Embed Size (px)

DESCRIPTION

Data Structure and Algorithms

Citation preview

Slide 1

Data Structure and AlgorithmsContentsAbstract Data TypesComplexity AnalysisHashingIntroduction to Graphs and TreesShortest Paths: Dijkstras AlgorithmMinimum Spanning Trees: Prims and Kruskals Algorithm2ContentsAbstract Data TypesStackQueue and Priority QueueLinked ListsComplexity AnalysisHashingIntroduction to Graphs and TreesShortest Paths: Dijkstras AlgorithmMinimum Spanning Trees: Prims and Kruskals Algorithm3StackA stack is a linear data structure which allows access to only one data item: the last item inserted [1, 2].The last tray put on the stack is the first tray removed from the stack.Stack is called an LIFO structure: Last in First out.4Operators of Stack in STLClear( ) Clear the stack.empty() Check to see if the stack is empty.push(el) put the element el on the topmost of the stack.pop() Remove the topmost element from the stack.top() Return the topmost element in the stack without removing.5Operation Executed on a Stack6Push (10)Push (5)Pop()Push (15)Pop ()1051010Push (7)1510715101510ContentsAbstract Data TypesStackQueue and Priority QueueLinked ListsComplexity AnalysisHashingIntroduction to Graphs and TreesShortest Paths: Dijkstras AlgorithmMinimum Spanning Trees: Prims and Kruskals Algorithm7QueueA queue is simple a waiting line that grows by adding elements to the end and shrinks by taking elements from the front.Queue is an FIFO structure: First in First out.8Queue Operations in STLback() return the last element.empty() return true if the queue includes no element and false otherwise.front() return the first element.pop() remove the first element.push(el) insert el at the end of queue.size() return the number of elements.

9Operation Executed on a Queue1010Push (10)Push (5)Pop()Push (15)Pop ()Push (7)10555155157157Priority QueuePriority queue is exactly like a regular queueor stackdata structure, but additionally, each element is associated with a priority.Priority queue: elements are pulled highest-priority or lowest-priority first.11Operations of Priority Queue431093026328911250143109302501632891FrontRearFrontRear109302501632891FrontRear43ContentsAbstract Data TypesStackQueue and Priority QueueLinked ListsComplexity AnalysisHashingIntroduction to Graphs and TreesShortest Paths: Dijkstras AlgorithmMinimum Spanning Trees: Prims and Kruskals Algorithm13Linked ListsA linked list is a collection of nodes storing data and links to other nodes.Nodes can be located anywhere in memory, and linked from one node to another by storing the address.14Links in a list15firstDataNextDataNextDataNextNullLinked ListLinkLinkLinkDataNextLinkInserting a New Link16firstDataNextDataNextDataNextNullLinked ListLinkLinkLinkDataNextLinka. Before InsertionLinked ListfirstDataNextDataNextDataNextNullLinkLinkLinkDataNextLinkDataNextb. After InsertionDeleting a Link17firstDataNextDataNextDataNextNullLinked ListLinkLinkLinkDataNextLinka. Before deletingLinked ListfirstDataNextDataNextNullLinkLinkLinkDataNextLinkb. After deletingContentsAbstract Data TypesComplexity AnalysisHashingIntroduction to Graphs and TreesShortest Paths: Dijkstras AlgorithmMinimum Spanning Trees: Prims and Kruskals Algorithm18Computational ComplexityA measure of resources in terms of Time and SpaceIf A is an algorithm that solves a decision problem f then run time of A is the number of steps taken on the input of length n.Time Complexity T(n) of a decision problem f is the run time of the best algorithm A for f.Space Complexity S(n) of a decision problem f is the amount of memory used by the best algorithm A for f.19Big-O NotationThe Big-O indicates the time or the memory needed.The Big-O notation is used to give an approximation to run-time-efficiency of an algorithm.

20The Big-O of an Algorithm AIf an algorithm A requires time proportional to f(n), then the algorithm A is said to be of order f(n), and it is denoted as O(f(n)).If an algorithm A requires time proportional to n2, then order of the algorithm is said to be O(n2).If an algorithm A requires time proportional to n, then order of the algorithm is said to be O(n).

Similarly, for algorithms having performance complexity O(log2(n)), O(n3),21Example1-D array, determine the Big-O of an algorithm;

Determine the Big-O:Ignoring constants such as 2 and 3, the algorithm is of the order n.The Big-O of algorithm is O(n)22Line noInstructionsNo of execution stepsline 1sum = 0;1line 2for ( i = 0; i < n; ++i )n+1line 3 sum += a[ I ];nline 4cout