Important Problem Types and Fundamental Data Structures

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Lecture 15. Graph Algorithms
2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.1 MAT 7003 : Mathematical Foundations (for Software Engineering) J Paul Gibson,
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Important Problem Types and Fundamental Data Structures
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org Some of the sides are exported from different sources to.
Introduction to Algorithms
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Graphs Upon completion you will be able to:
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Final Exam Review COP4530.
Chapter 12 Abstract Data Type.
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Data Structures Michael J. Watts
Top 50 Data Structures Interview Questions
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
CSE 373 Topological Sort Graph Traversals
Chapter 5 : Trees.
Heaps, Heap Sort and Priority Queues
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Csc 2720 Instructor: Zhuojun Duan
Hashing Exercises.
Program based on pointers in C.
Binary Trees, Binary Search Trees
UCS 406 – Data Structures & Algorithms
Introduction to Trees IT12112 Lecture 05.
Chapter 1.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
i206: Lecture 14: Heaps, Graphs intro.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Introduction to Data Structures
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
CMSC 202 Trees.
Design and Analysis of Algorithms
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Trees.
A Introduction to Computing II Lecture 13: Trees
Design and Analysis of Algorithms
DATA STRUCTURE.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Data Structures and Algorithm Analysis Priority Queues (Heaps)
OPIM 915 Fall 2010 Data Structures 23-38,
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Heaps Chapter 6 Section 6.9.
Presentation transcript:

Important Problem Types and Fundamental Data Structures Important problems types Sorting, searching, string processing, graph problems Fundamental data structures Linear data structures Stacks, queues, and heaps Graphs Trees

Sorting Rearrange the items of a given list in ascending/descending order. Input: A sequence of n numbers <a1, a2, …, an> Output: A reordering <a´1, a´2, …, a´n> of the input sequence such that a´1≤ a´2 ≤ … ≤ a´n. Why sorting? Help searching Algorithms often use sorting as a key subroutine. Sorting key A specially chosen piece of information used to guide sorting. I.e., sort student records by names.

Sorting Examples of sorting algorithms Bubble sort Selection sort Insertion sort Merge sort Quick sort Heap sort … Evaluate sorting algorithm complexity: the number of key comparisons. Two properties Stability: A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input. In place : A sorting algorithm is in place if it does not require extra memory, except, possibly for a few memory units.

Searching Find a given value, called a search key, in a given set. Examples of searching algorithms Sequential searching Binary searching…

String Processing A string is a sequence of characters from an alphabet. Text strings: letters, numbers, and special characters. String matching: searching for a given word/pattern in a text.

Graph Problems Informal definition A graph is a collection of points called vertices, some of which are connected by line segments called edges. Graphs can be used for modeling real-life applications Estimation of web diameter, which is maximum no. of links one needs to follow to reach one web page. communication networks Project scheduling games Examples of graph algorithms Graph traversal algorithms Shortest-path algorithms Topological sorting

Linear Data Structures Arrays fixed length (need preliminary reservation of memory) contiguous memory locations direct access Insert/delete Linked Lists dynamic length arbitrary memory locations access by following links Arrays A sequence of n items of the same data type that are stored contiguously in computer memory and made accessible by specifying a value of the array’s index. Linked List A sequence of zero or more elements called nodes each containing two kinds of information: some data and one or more links called pointers to other nodes of the linked list. Singly linked list (next pointer) Doubly linked list (next + previous pointers)

Singly linked list of n elements Array of n elements Singly linked list of n elements Double linked list of n elements

Stacks, Queues, and Heaps (1) A stack of plates insertion/deletion can be done only at the top. LIFO/FILO Two operations (push and pop) Queues A queue of customers waiting for services Insertion/enqueue from the rear and deletion/dequeue from the front. FIFO Two operations (enqueue and dequeue)

Stacks, Queues, and Heaps (2) Priority queues (implemented using heaps) A data structure for maintaining a set of elements, each associated with a key/priority, with the following operations Finding the element with the highest priority Deleting the element with the highest priority Inserting a new element Scheduling jobs on a shared computer.

Graphs Formal definition (a) (b) A graph G = <V, E> is defined by a pair of two sets: a finite set V of items called vertices and a set E of pairs of vertex called edges. If these pairs of vertices are unordered, we say that the graph G is undirected otherwise directed graphs (digraph). A graph with every pair of its vertices connected by an edge is called complete. K|V| A graph with relatively few possible edges missing is called dense. A graph with few edges relative to the no. of its vertices is called sparse. (a) (b)

Graph Representation Adjacency matrix Adjacency linked lists Graphs for computer algorithm can be represented in two principal ways: Adjacency matrix The adjacency matrix of a graph with n vertices is an n-by-n boolean matrix with one row and one column for each of the graph’s vertices. The element on the ith row and jth column is 1 if there’s an edge from ith vertex to the jth vertex; otherwise 0. The adjacency matrix of an undirected graph is symmetric. Adjacency linked lists A collection of linked lists, one for each vertex, that contain all the vertices adjacent to the list’s vertex.

Weighted Graphs A weighted graph is a graph or digraphs with numbers assigned to its edges. These numbers are called weights or costs. Used to find shortest path between two points.

Graph Properties -- Paths and Connectivity A path from vertex u to v of a graph G can be defined as a sequence of adjacent (connected by an edge) vertices that starts with u and ends with v. If all edges of a path are distinct, the path is said to be simple. Path lengths: the number of edges, or the number of vertices – 1. Connected graphs A graph is said to be connected if for every pair of its vertices u and v there is a path from u to v. Connected component The maximum connected subgraph of a given graph. Examples of a simple path and a not simple path. Connected graphs: starting from any vertex, we can always find a path to reach all the other vertices. (Ball-String example.) From NIST: Connected graphs: Definition: An undirected graph that has a path between every pair of vertices. Strongly connected graphs: Definition: A directed graph that has a path from each vertex to every other vertex. Connected component: … Strongly connected component: a strongly connected component of a digraph G is a maximal strongly connected subgraph of G. Graph that is not connected

Graph Properties -- Acyclicity Cycle A cycle is a simple path of a positive length that starts and ends a the same vertex. For example, f, h, i, g, f is a cycle in the graph of above figure. Acyclic graph A graph with no cycles is said to be acyclic. DAG (Directed Acyclic Graph)

Trees Trees Properties of trees A tree (or free tree) is a connected acyclic graph. A graph that has no cycles but is not necessarily connected is called a forest. Properties of trees Trees have several important properties other graphs do not have. In particular, the number of edges in a tree is always one less than the number of its vertices: |E| = |V| - 1 For every two vertices in a tree there always exists exactly one simple path from one of these vertices to the other. Why? Rooted trees: The above property makes it possible to select an arbitrary vertex in a free tree and consider it as the root of the so-called rooted tree. Levels of rooted tree.

A tree and a forest

Rooted Trees ancestors descendants parent, child and siblings Leaves For any vertex v in a tree T, all the vertices on the simple path from the root to that vertex are called ancestors. descendants All the vertices for which a vertex v is an ancestor are said to be descendants of v. parent, child and siblings If (u, v) is the last edge of the simple path from the root to vertex v (and u  v), u is said to be the parent of v and v is called a child of u. Vertices that have the same parent are called siblings. Leaves A vertex without children is called a leaf. Subtree A vertex v with all its descendants is called the subtree of T rooted at v.

Transformation of a free tree into a rooted tree

Trees Depth of a vertex The length of the simple path from the root to the vertex. Height of a tree The length of the longest simple path from the root to a leaf. For example the depth of vertex c of the above tree is 2, and the height of the tree is 3.

Ordered Trees Ordered trees Binary trees Binary search trees An ordered tree is a rooted tree in which all the children of each vertex are ordered. Binary trees A binary tree is an ordered tree in which every vertex has no more than two children and each children is designated as either a left child or a right child of its parent. Binary search trees Each vertex is assigned a number. A number assigned to each parental vertex is larger than all the numbers in its left subtree and smaller than all the numbers in its right subtree. log2n  h  n – 1, where h is the height of a binary tree.

A binary tree A binary search tree Standard implementation of (b)

A rooted tree and its first child-next sibling representation Here, the left pointer will point to the first child of the vertex while the right pointer will point to its next sibling. Accordingly this representation is called the first child-next sibling representation.