Topological Sort and Hashing

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Chapter 9: Graphs Topological Sort
Some Graph Algorithms.
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Topological Sort.
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Hashing Techniques.
Hashing CS 3358 Data Structures.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
Hashing Hashing is another method for sorting and searching data.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Topological Sort: Definition
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Topological Sorting.
Hashing Exercises.
CS120 Graphs.
More Graph Algorithms.
Topological Sort.
Topological Sort.
"Learning how to learn is life's most important skill. " - Tony Buzan
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Directed Graph Algorithms
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
ITEC 2620M Introduction to Data Structures
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CSE 373: Data Structures and Algorithms
Hashing.
Richard Anderson Lecture 5 Graph Theory
Analysis and design of algorithm
Presentation transcript:

Topological Sort and Hashing Presented by Phillip Nguyen Truong Nguyen

Topological Sort A topological sort of a directed graph is one in which if there is a path from v to w, v comes before w in the ordering The graph must be acyclic (no cycles exist) In a graph with no cycles, there must always be at least one vertex with 0 indegree, we will start at one of these vertices

Applications A topological sort is useful in something like a course prerequisite system, where you know what classes you need to take before you can take others Planning and scheduling. Building houses.

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. B C A F D E

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. B C A A B F C D E F D E

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. Any linear ordering in which all the arrows go to the right. B C A A B F C D E F D E

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. Any linear ordering in which all the arrows go to the right. B C A A B E C D F F D E This is not a topological ordering.

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—why?) B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—because the graph is acyclic.) B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. Select one of them. B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Remove it, and its outgoing edges, and add it to the output. B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, . . . B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, . . . B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. B C A F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. C A F B D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. C A F B D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort The topological sorting algorithm: finished! B C A A F B C D E F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound? B C A A F B C D E F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: ? Place vertices in output: ? B C A A F B C D E F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: O(|E|) Place vertices in output: ? B C A A F B C D E F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: O(|E|) Place vertices in output: O(|V|) B C A A F B C D E F D E

Graph Algorithms: Topological Sort Find vertices with no predecessors: ? Assume an adjacency list representation: A B C D E F B D B C C A D E F E D E

Graph Algorithms: Topological Sort The topological sorting algorithm: …and initialize and maintain for each vertex its no. of predecessors. A B C D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

Graph Algorithms: Topological Sort Find vertices with no predecessors: ? Time for each vertex: O(|V|) B A B C D E F D B 1 C C A 1 D E F 2 E 2 D E

Graph Algorithms: Topological Sort Find vertices with no predecessors: ? 2 Total time: O(|V| ) B A B C D E F D B 1 C C A 1 D E F 2 E 2 D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2 2 Total: O(|V| + |E|)

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2 2 Total: O(|V| + |E|) Too much!

Graph Algorithms: Topological Sort The topological sorting algorithm: We need a faster way to do this step: Find vertices with no predecessors.

Graph Algorithms: Topological Sort The topological sorting algorithm: Key idea: initialize and maintain a queue (or stack) holding pointers to the vertices with 0 predecessors A B C D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. No scan is required, so O(1). A B C D E F 1 B C C 1 D E F 1 E 1 2 2 D E Output: A

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F 1 B C C 1 D E 1 E 1 2 2 D E Output: A F

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F C D E 1 E 1 2 2 D E Output: A F B

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F E 1 1 D E Output: A F B C

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F E Output: A F B C D

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. Finished! A B C D E F Output: A F B C D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Now the time for each part is Find vertices with no predecessors: O(|V|) Remove edges: O(|E|) Place vertices in output: O(|V|) Total: O(|V|+|E|) Linear in |V|+|E|. Much better!

Hashing The implementation of hash tables is called hashing. Hashing is a technique used for performing insertions, deletions, and finds.

Binary Search Trees will give us a find operation or insert operation in O(log n). There is a way to improve on this. We will create a table of size TableSize which we will index from 0 to TableSize - 1. Each element that we want to store has a “key” which is some portion of the data that we use to establish the order of the data items. We must have a function that can take a key as input and produce an integer in the range from 0 to TableSize - 1. This function is called a hash function.

Sample Hash functions If the key is an integer. One choice is to take h(key) = key % TableSize One of the things people do is to choose TableSize to be a prime number.

The hash function gives us an index into the array. Open addressing In this method of implementing a hash table, we don’t have linked lists. Instead we just have an array of items to search. The hash function gives us an index into the array. In the event that collision occurs, there are two common strategies linear probing quadratic probing

Linear Probing Alice Ann Cynthia Jane Julia June Laura Sue Valerie Vanessa

Instead of using linear probing, we can adopt a different Quadratic probing Instead of using linear probing, we can adopt a different strategy called quadratic probing. If a collision occurs at a location h(x), then we try h(x) + 1,then h(x) + 4, h(x) + 9, ... (modulo the size of the table). The major result about quadratic probing is: Theorem. If quadratic probing is used and the table size is prime, then a new element can always be inserted if the table is at least half empty.

keys: 89, 18, 49, 58, 69 Empty table After 89 After 18 After 49 After 58 After 69 49 49 49 1 2 58 58 3 69 4 5 6 7 8 18 18 18 18 9 89 89 89 89 89 Quadratic Probing

THE END