gyan from the geek apoorva vadi m.s information systems new york university

29
Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Upload: roxana-bryer

Post on 29-Mar-2015

234 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Gyan from the Geek

Apoorva VadiM.S Information SystemsNew York University

Page 2: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University
Page 4: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Lets talk about M&M?The Math Monster

Page 5: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

BAD MATH !!!!

Page 6: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University
Page 7: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Algorithms…

What are those things?

Page 8: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

An algorithm…is like a recipe

Problem solving as an analogy to cooking:1. Inputs2. Recipe/Set of defined rules (or an Algorithm)3. Outputs

Page 9: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Now lets use algorithms…

To solve Puzzles!• Towers of Hanoi• Dining Philosophers• Travelling salesman• Eight queens

Page 10: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Towers of Hanoi

You have a stack of discs, from largest to smallest, that slide on to the first peg of a three peg board. Your goal is to move the entire stack of discs from the first peg to the third peg. However, you can only move the topmost disc of any peg, and smaller discs must always be placed on larger discs. How many moves will it take?

Page 11: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University
Page 12: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Now lets write a recipe for this…Alternating between the smallest and the next-smallest disks, follow the steps for the

appropriate case:

• For an even number of disks:• make the legal move between pegs A and B• make the legal move between pegs A and C• make the legal move between pegs B and C• repeat until complete

For an odd number of disks:• make the legal move between pegs A and C• make the legal move between pegs A and B• make the legal move between pegs B and C• repeat until complete• In each case, a total of 2n-1 moves are made.

Page 13: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

But that was too long… lets try recursion…

Step 1: Move N−1 discs from A to B. This leaves Nth disc alone on peg A.

Step 2: Move Nth disc from A to CStep 3: Move N−1 discs from B to C so they sit

on disc N.

Page 14: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Dining Philosophers Problem

Lets try something ‘non-arkymalarky’:Five philosophers sit around a circular table. In front of each philosopher is a large plate of rice. The philosophers alternate their time between eating and thinking. There is one chopstick between each philosopher, to their immediate right and left. In order to eat, a given philosopher needs to use both chopsticks. How can you ensure all the philosophers can eat reliably without starving to death?

Page 15: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

So this is a classic example of a common computing problem in

concurrency…Issues:• Deadlock - cycle of unwarranted requests. Every

philosopher picked up a left fork and waits for a right fork (forever).

• Resource Starvation – one philosopher might have to wait extended amounts of time.

• Mutual exclusion – multiple processes accessing sets of data.

Page 16: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University
Page 17: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Welcome to the world of Data Structures

• Stacks• Queues• Linked Lists• Trees

Have Fun

Page 18: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Applications of Stacks

• Direct applicationsPage-visited history in a Web browsUndo sequence in a text editor

• Indirect applicationsComponent of other data structures

Page 19: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Applications of Queues

• Direct applicationWaiting lines Access to shared resources (e.g.,

printer) Multiprogramming

• Indirect applications Component of other data structures

Page 20: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

List

•A singly linked list is a concrete data structureconsisting of a sequence of nodes -Each node stores element -link to the next node

Page 21: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

• We can implement a queue with a singly linked list -The front element is stored at the first node

-The rear element is stored at the last node

Queue with a Singly Linked List

Page 22: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Doubly Linked List

• A doubly linked list provides a natural implementation of the List ADT

• Nodes implement Position and store:-element-link to the previous node-link to the next node

• Special trailer and header nodes

Page 23: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Doubly Linked List

Page 24: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Trees•In computer science, a tree is an abstract model of a hierarchicalstructure -A tree consists of nodes with a parent-childrelation•Applications: -Organization charts -File systems -Programming environments

Page 25: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Binary Trees• A binary tree is a tree with the following properties:

-Each internal node has two children-The children of a node are an ordered pair- We call the children of an internal

• node left child and right child- Alternative recursive definition: a

• binary tree is either-a tree consisting of a single node,

or- a tree whose root has an ordered pair of children, each of which is a binary tree

• Applications:-arithmetic expressions-decision processes

-searching

Page 26: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Binary Tree

Page 27: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

Tree Traversals(power of recursion)

Depth First Search• Pre-order(NLR): Root Node- Left child- Right child

A-B-D-E-H-I-C-F-G• In-order(LNR): Left child - Root Node - Right child

D-B-H-E-I-A-F-C-G• Post-order(LRN): Left child - Right child – Root Node

D-H-I-E-B-F-G-C-ABreadth First Traversal• Level order Traversal: Traverse each node level by level A-B-C-D-E-F-G-H-I

Page 29: Gyan from the Geek Apoorva Vadi M.S Information Systems New York University

That’s all folks !