Hashing with Separate Chaining Implementation – data members public class SCHashTable implements HashTableInterface { private List [] table; private int.

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

22C:19 Discrete Math Graphs Fall 2010 Sukumar Ghosh.
22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Graph-02.
Data Structures Using C++
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Using arrays – Example 2: names as keys How do we map strings to integers? One way is to convert each letter to a number, either by mapping them to 0-25.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Implementation of Linear Probing (continued) Helping method for locating index: private int findIndex(long key) // return -1 if the item with key 'key'
Graphs. John Edgar  Understand graph terminology  Implement graphs using  Adjacency lists and  Adjacency matrices  Perform graph searches  Depth.
Graph & BFS.
Using nextAdjacent() To list all vertices connected to a vertex u in graph G we can use the following loop: for (int v=G.nextAdjacent(u,-1); v>=0; v=G.nextAdjacent(u,v))
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs. Graph A “graph” is a collection of “nodes” that are connected to each other Graph Theory: This novel way of solving problems was invented by a.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
22C:19 Discrete Math Graphs Spring 2014 Sukumar Ghosh.
GRAPH Learning Outcomes Students should be able to:
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
Graphs Edge(arc) Vertices can be even or odd or undirected (two-way) Edges can be directed (one-way) This graph is connected. Degree(order) = 3 Odd vertex.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Lecture 11: 9.4 Connectivity Paths in Undirected & Directed Graphs Graph Isomorphisms Counting Paths between Vertices 9.5 Euler and Hamilton Paths Euler.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Chapter 9: Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Graphs Chapter 20.
Data Structures Graphs - Terminology
Introduction to Graphs
Graphs.
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
Introduction (with applications) ADT & terminology
Data Structures and Algorithms for Information Processing
Discrete Maths 9. Graphs Objective
Graphs Chapter 13.
ITEC 2620M Introduction to Data Structures
Graph Theory What is a graph?.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Graph Theory Relations, graphs
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

Hashing with Separate Chaining Implementation – data members public class SCHashTable implements HashTableInterface { private List [] 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=0; i<size; i++) table[i] = new List (); }

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

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

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

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

Comparing hash tables and balanced BSTs With good hash function and load kept low, hash tables perform insertions, deletions and search in O(1) 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(1) 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 weighted graph

Graph Theory and Euler The Swiss mathematician Leonhard Euler invented graph theory in the 1700’s One problem he solved (in 1736) 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 1945 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 0..|V|-1. For simplicity, we will assume that vertices are integers 0..|V|-1 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 1 when there is an edge between vertices i and j or 0 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(1) time Finding all vertices adjacent to a given vertex requires O(  V  ) time The matrix requires  V  2 space

Adjacency Matrix Examples ABCDEFG A B C D E F G BAC DE GF ABCDEFG A  1  3  5  B  2  C51  D1  E  2  3 F  8 G  24  BAC DE GF

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 = 0; 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<0, return the first adjacent vertex // otherwise, return next one after w // if none exist, return -1 { for (int i=w+1; i<numVertices; i++) if (isEdge(v,i)) return i; return -1; } } // end Graph