Hashing with Separate Chaining Implementation data members

Similar documents
V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

An Introduction to APGL

Social Media Mining. Graph Essentials

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Euler Paths and Euler Circuits

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Ordered Lists and Binary Trees

CompSci-61B, Data Structures Final Exam

Data Structures and Algorithms Written Examination

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Discrete Mathematics & Mathematical Reasoning Chapter 10: Graphs

Asking Hard Graph Questions. Paul Burkhardt. February 3, 2014

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Data Structures in Java. Session 15 Instructor: Bert Huang

Data Structures and Algorithms

DATA STRUCTURES USING C

A Comparison of Dictionary Implementations

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Class One: Degree Sequences

CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions. Linda Shapiro Spring 2016

Network (Tree) Topology Inference Based on Prüfer Sequence

B AB 5 C AC 3 D ABGED 9 E ABGE 7 F ABGEF 8 G ABG 6 A BEDA 3 C BC 1 D BCD 2 E BE 1 F BEF 2 G BG 1

Graph theory and network analysis. Devika Subramanian Comp 140 Fall 2008

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

Java Software Structures

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Graph Theory Origin and Seven Bridges of Königsberg -Rhishikesh

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Mining Social Network Graphs

Midterm Practice Problems

Binary Heap Algorithms

Part 2: Community Detection

Sample Questions Csci 1112 A. Bellaachia

CMPSCI611: Approximating MAX-CUT Lecture 20

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Chapter 6: Graph Theory

3. Eulerian and Hamiltonian Graphs

Analysis of Algorithms, I

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Algorithms and Data Structures Written Exam Proposed SOLUTION

Any two nodes which are connected by an edge in a graph are called adjacent node.

Simple Graphs Degrees, Isomorphism, Paths

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Directed Graphs. digraph search transitive closure topological sort strong components. References: Algorithms in Java, Chapter 19

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Reductions & NP-completeness as part of Foundations of Computer Science undergraduate course

SCAN: A Structural Clustering Algorithm for Networks

NP-Completeness. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Approximation Algorithms

Exam study sheet for CS2711. List of topics

5. Binary objects labeling

Networks and Paths. The study of networks in mathematics began in the middle 1700 s with a famous puzzle called the Seven Bridges of Konigsburg.

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Practical Graph Mining with R. 5. Link Analysis

A Fast Algorithm For Finding Hamilton Cycles

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Efficient Data Structures for Decision Diagrams

DATA ANALYSIS II. Matrix Algorithms

Data Structure [Question Bank]

Introduction to Graph Theory

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

Lecture 1: Course overview, circuits, and formulas

Lecture Notes on Binary Search Trees

KEYWORD SEARCH OVER PROBABILISTIC RDF GRAPHS

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Binary Trees and Huffman Encoding Binary Search Trees

Dynamic Programming. Lecture Overview Introduction

Binary Search Trees (BST)

International Journal of Software and Web Sciences (IJSWS)

Solutions to Homework 6

Warshall s Algorithm: Transitive Closure

Finding and counting given length cycles

Zachary Monaco Georgia College Olympic Coloring: Go For The Gold

Cloud Computing. Lectures 10 and 11 Map Reduce: System Perspective

Persistent Binary Search Trees


One last point: we started off this book by introducing another famously hard search problem:

Lecture 7: NP-Complete Problems

A Sublinear Bipartiteness Tester for Bounded Degree Graphs

Converting a Number from Decimal to Binary

Load balancing Static Load Balancing

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

An Introduction to The A* Algorithm

GCE Computing. COMP3 Problem Solving, Programming, Operating Systems, Databases and Networking Report on the Examination.

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

Network/Graph Theory. What is a Network? What is network theory? Graph-based representations. Friendship Network. What makes a problem graph-like?

Just the Factors, Ma am

PES Institute of Technology-BSC QUESTION BANK

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Graphical degree sequences and realizations

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

8.1 Min Degree Spanning Tree

Persistent Data Structures and Planar Point Location

The GeoMedia Fusion Validate Geometry command provides the GUI for detecting geometric anomalies on a single feature.

Social Media Mining. Network Measures

COMPUTER SCIENCE. Paper 1 (THEORY)

Transcription:

Hashing with Separate Chaining Implementation data members public class SCHashTable<T extends KeyedItem> implements HashTableInterface<T> { private List<T>[] table; private int h(long key) // hash function // return index { return (int)(key % table.length); // typecast to int } public SCHashTable(int size) // recommended size: prime number roughly twice bigger // than the expected number of elements { table = new List[size]; // initialize the lists for (int i=; i<size; i++) table[i] = new List<T>(); }

Implementation insertion public void insert(t item) { int index = h(item.getkey()); List<T> L = table[index]; // insert item to L L.add(,item); // if linked list is used, // insertion will be efficient }

Implementation search private int findindex(list<t> L, long key) // search for item with key 'key' in L // return - if the item with key 'key' was not found in L { // search of item with key = 'key' for (int i=; i<=l.size(); i++) if (L.get(i).getKey() == key) return i; } return -; // not found public T find(long key) { int index = h(key); List<T> L = table[index]; int list_index = findindex(l,key); if (index>=) return L.get(list_index); else return null; // not found }

Implementation deletion public T delete(long key) { int index = h(key); List<T> L = table[index]; int list_index = findindex(l,key); if (index>=) { T item = L.get(list_index); L.remove(list_index); return item; } else return null; // not found }

Hashing comparison of different methods Figure: The relative efficiency of four collision-resolution methods

Comparing hash tables and balanced BSTs With good hash function and load kept low, hash tables perform insertions, deletions and search in O() time on average, while balanced BSTs in O(log n) time. However, there are some tasks (order related) for which, hash tables are not suitable: traversing elements in sorted order: O(N+n.log n) vs. O(n) finding minimum or maximum element: O(N) vs. O() range query: finding elements with keys in an interval [a,b]: O(N) vs. O(log n + s), s is the size of output Depending on what kind of operations you will need to perform on the data and whether you need guaranteed performance on each query, you should choose which implementation to use.

CMPT 225 Graphs

Graph Terminology A graph consists of two sets A set V of vertices (or nodes) and A set E of edges that connect vertices V is the size of V, E the size of E A path (walk) between two vertices is a sequence of edges that begins at one vertex and ends at the other A simple path (path) is one that does not pass through the same vertex more than once A cycle is a path that begins and ends at the same vertex

Connected Graphs A connected graph is one where every pair of distinct vertices has a path between them A complete graph is one where every pair of vertices has an edge between them A graph cannot have multiple edges between the same pair of vertices A graph cannot have loops [a loop = an edge from and to the same vertex] connected graph complete graph disconnected graph

Directed Graphs In a directed graph (or digraph) each edge has a direction and is called a directed edge A directed edge can only be traveled in one direction A pair of vertices in a digraph can have two edges between them, one in each direction directed graph

Weighted Graphs In a weighted graph each edge is assigned a weight Edges are labeled with their weights Each edge s weight represents the cost to travel along that edge The cost could be distance, time, money or some other measure The cost depends on the underlying problem 3 3 2 4 2 2 5 3 weighted graph

Graph Theory and Euler The Swiss mathematician Leonhard Euler invented graph theory in the 7 s One problem he solved (in 736) was the Konigsberg bridge problem Konigsberg was a city in Eastern Prussia which had seven bridges in its centre Konigsberg was renamed Kalinigrad when East Prussia was divided between Poland and Russia in 945 The inhabitants of Konigsberg liked to take walks and see if it was possible to cross each bridge once and return to where they started Euler proved that it was impossible to do this, as part of this proof he represented the problem as a graph

Konigsberg

Konigsberg Graph

Multigraphs The Konigsberg graph is an example of a multigraph A multigraph has multiple edges between the same pair of vertices In this case the edges represent bridges

Graph Uses Graphs are used as representations of many different types of problems Network configuration Airline flight booking Pathfinding algorithms Database dependencies Task scheduling Critical path analysis Garbage collection in Java etc.

Basic Graph Operations Create an empty graph Test to see if a graph is empty Determine the number of vertices in a graph Determine the number of edges in a graph Determine if an edge exists between two vertices and in a weighted graph determine its weight Insert a vertex each vertex is assumed to have a distinct search key Delete a vertex, and its associated edges Delete an edge Return a vertex with a given key

Graph Implementation There are two common implementation of graphs Both implementations require to map a vertex (key) to an integer.. V -. For simplicity, we will assume that vertices are integers.. V - and cannot be added or deleted. The implementations record the set of edges differently Adjacency matrices provide fast lookup of individual edges but waste space for sparse graphs Adjacency lists are more space efficient for sparse graphs and find all the vertices adjacent to a given vertex efficiently

Adjacency Matrix The edges are recorded in an V * V matrix In an unweighted graph entries in matrix[i][j] are when there is an edge between vertices i and j or when there is no edge between vertices i and j In a weighted graph entries in matrix[i][j] are either the edge weight if there is an edge between vertices i and j or infinity when there is no edge between vertices i and j Looking up an edge requires O() time Finding all vertices adjacent to a given vertex requires O( V ) time The matrix requires V 2 space

Adjacency Matrix Examples G F E D C B A G F E D C B A B A C D E G F 4 2 G 8 F 3 2 E D 5 C 2 B 5 3 A G F E D C B A B A C D E G F 4 5 8 2 2 5 3 2 3

Implementation with adjacency matrix class Graph { // simple graph (no multiple edges); undirected; unweighted private int numvertices; private int numedges; private boolean[][] adjmatrix; public Graph(int n) { numvertices = n; numedges = ; adjmatrix = new boolean[n][n]; } // end constructor public int getnumvertices() { return numvertices; } // end getnumvertices public int getnumedges() { return numedges; } // end getnumedges public boolean isedge(int v, int w) { return adjmatrix[v][w]; } // end isedge

public void addedge(int v, int w) { if (!isedge(v,w)) { adjmatrix[v][w] = true; adjmatrix[w][v] = true; numedges++; } } // end addedge public void removeedge(int v, int w) { if (isedge(v,w)) { adjmatrix[v][w] = false; adgmatrix[w][v] = false; numedges--; } } // end removeedge public int nextadjacent(int v,int w) // if w<, return the first adjacent vertex // otherwise, return next one after w // if none exist, return - { for (int i=w+; i<numvertices; i++) if (isedge(v,i)) return i; return -; } } // end Graph