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

ITEC 2620M Introduction to Data Structures

Embed Size (px)

DESCRIPTION

ITEC 2620M Introduction to Data Structures. Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: TEL 3049. Graphs. Key Points of this Lecture. Graph Algorithms Definitions, representations, analysis Shortest paths Minimum-cost spanning tree. - PowerPoint PPT Presentation

Citation preview

Page 1: ITEC 2620M Introduction to Data Structures

ITEC 2620MIntroduction to Data Structures

Instructor: Prof. Z. YangCourse Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: TEL 3049

Page 2: ITEC 2620M Introduction to Data Structures

Graphs

Page 3: ITEC 2620M Introduction to Data Structures

3

Key Points of this Lecture

• Graph Algorithms– Definitions, representations,

analysis– Shortest paths– Minimum-cost spanning tree

Page 4: ITEC 2620M Introduction to Data Structures

4

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 neighbours

• 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

Page 5: ITEC 2620M Introduction to Data Structures

5

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

(useful for unidirectional relationships

Page 6: ITEC 2620M Introduction to Data Structures

6

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 vi, 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 vi.

• Example

Page 7: ITEC 2620M Introduction to Data Structures

7

Comparisons and Analysis

• Space– adjacency matrix uses O(|V|2) 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) • adjacency list – O(d)

– 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

Page 8: ITEC 2620M Introduction to Data Structures

8

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– assume weighted (undirected) connected

graph– use Dijkstra’s algorithm (a greedy

algorithm) • build paths from unvisited vertex with least

current cost