ITEC 2620M Introduction to Data Structures

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Review Binary Search Trees Operations on Binary Search Tree
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
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
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 2 Graph Algorithms.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
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++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graphs and Shortest Paths Using ADTs and generic programming.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
CS202 - Fundamental Structures of Computer Science II
May 3rd – Hashing & Graphs
Data Structures Using C++ 2E
Graphs Representation, BFS, DFS
Data Structures 13th Week
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
C.Eng 213 Data Structures Graphs Fall Section 3.
Data Structures Using C++ 2E
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
CS202 - Fundamental Structures of Computer Science II
Graph Algorithm.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
Graph Operations And Representation
Chapter 11 Graphs.
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 16 1 – Graphs Graph Categories Strong Components
Important Problem Types and Fundamental Data Structures
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Graph algorithms
GRAPHS.
Introduction to Graphs
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: DB 3049

Graphs

Key Points Graph Algorithms Definitions, representations, analysis Shortest paths Minimum-cost spanning tree

Basic Definitions A graph G = ( V, E ) consists of a set of vertices V and a set of edges E – each edge E connects a pair of vertices in V. Graphs can be directed or undirected. redraw above with arrows – first vertex is source Graphs may be weighted. redraw above with weights, combine definitions A vertex vi is adjacent to another vertex vj if they are connected by an edge in E. These vertices are neighbors. A path is a sequence of vertices in which each vertex is adjacent to its predecessor and successor. The length of a path is the number of edges in it. The cost of a path is the sum of edge weights in the path

Basic Definitions (Cont’d) A cycle is a path of length greater than one that begins and ends at the same vertex. A simple cycle is a cycle of length greater than three that does not visit any vertex (except the start/finish) more than once. Two vertices are connected if there as a path between them. A subset of vertices S is a connected component of G if there is a path from each vertex vi to every other distinct vertex vj in S. The degree of a vertex is the number of edges incident to it. – the number of vertices that it is connected to A graph is acyclic if it has no cycles (e.g. a tree) . A directed acyclic graph is called a DAG or digraph

Representations The adjacency matrix of graph G = ( V, E ) for vertices numbered 0 to n-1 is an n x n matrix M where M[i][j] is 1 if there is an edge from vi to vj, and 0 otherwise. The adjacency list of graph G = ( V, E ) for vertices numbered 0 to n-1 consists of an array of n linked lists. The ith linked list includes the node j if there is an edge from vi to vj. Example

Comparisons and Analysis Space adjacency matrix uses O( ) space (constant) adjacency list uses O(|V| + |E|) space (note: pointer overhead) better for sparse graphs (graphs with few edges) Access Time Is there an edge connecting vi to vj? adjacency matrix – O(1) adjacency list – O(d) Visit all edges incident to vi adjacency matrix – O(n) Primary operation of algorithm and density of graph determines more efficient data structure. complete graphs should use adjacency matrix traversals of sparse graphs should use adjacency list

Spanning Tree and Shortest Paths Minimum-Cost Spanning Tree assume weighted (undirected) connected graph use Prim’s algorithm (a greedy algorithm) from visited vertices, pick least-cost edge to an unvisited vertex Shortest Paths use Dijkstra’s algorithm (a greedy algorithm) build paths from unvisited vertex with least current cost

HASHING

Key Points Hash tables Hash functions Collision resolution and clustering Deletions

Indices vs. Keys Each key/record is associated with an array slot. We could map each key to each slot. e.g. last name to apartment number We could then search either the array (unsorted?) or a look-up table (sorted?) . However, what if the look-up is actually a calculated function? eliminate look-up!

Hash Functions A hash function h() converts a key (integer, string, float, etc) into a table index. Example

Hash Tables Records are stored in slots specified by a hash function. Look-up/store Convert key into a table index with hash function h() h(key) = index Find record/empty slot starting at index = h(key) (use resolution policy if necessary)

Comments Hash function should evenly distribute keys across table. not easy given unspecified input data distribution Hash table should be about half full. note: time-space tradeoff more space -> less time (and already twice as much space as a sorted array) if half full, 50% chance of one collision 25% chance of two collisions etc... 2 accesses on average (approaches n as table fills)

How to do better What to do with collisions? linear probing (“classic hashing”) if collision, search spaces sequentially To eliminate clustering, we would like each remaining slot to have equal probability. Can’t use random – needs to be reproducable. Pseudo-random probing (see text) Goal of random probing? --> cause divergence Probe sequences should not all follow same path.

Quadratic Probing Simple divergence method Linear probing – ith probe is i slots away Quadratic probing

Secondary Clustering If multiple keys are hashed to the same index/home position, quadratic probing still follows the same path each time. This is secondary clustering Use second hash function to determine probe sequence.