DATA STRUCTURES AND ALGORITHMS

19
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI

description

DATA STRUCTURES AND ALGORITHMS. Lecture Notes 10 Prepared by İnanç TAHRALI. ROAD MAP. GRAPH ALGORITHMS Definition Representation of Graphs Topological Sort. GRAPHS. A graph is a tuple G=(V,E) V : set of vertices / nodes E : set of edges each edge is a pair ( v,w ), where v,w Є V - PowerPoint PPT Presentation

Transcript of DATA STRUCTURES AND ALGORITHMS

Page 1: DATA STRUCTURES  AND ALGORITHMS

DATA STRUCTURES

ANDALGORITHMS

Lecture Notes 10

Prepared by İnanç TAHRALI

Page 2: DATA STRUCTURES  AND ALGORITHMS

2

ROAD MAP GRAPH ALGORITHMS

Definition Representation of Graphs Topological Sort

Page 3: DATA STRUCTURES  AND ALGORITHMS

3

GRAPHS A graph is a tuple G=(V,E)

V : set of vertices /nodes E : set of edges

each edge is a pair (v,w), where v,w Є V If the pairs are ordered, then the

graph is directed directed graphs are sometimes refered as

digraphs Vertex w is adjacent to v iff (v,w) Є E

Page 4: DATA STRUCTURES  AND ALGORITHMS

4

GRAPHS A path is a sequence of vertices

w1, w2, …, wN where (wi, wi+1) Є E for 1≤i<N The length of a path is the number of edges on

the path, which is equal to N-1 if path contains no edges, length is 0

If the graph contains an edge (v,v) from a vertex to itself, then the path (v,v) is sometimes refered to a loop.

A simple path is a path such that all vertices are distinct, except that the first and last could be the same

Page 5: DATA STRUCTURES  AND ALGORITHMS

5

GRAPHS A cycle is a path with length ≥ 1 where w1 = wN

Cycle is simple, if the path is simple In an undirected graph edges should be distinct for

simple cycle A directed graph is acyclic if it has no cycles

A directed acyclic graph is refered to DAG An undirected graph is connected if there is a

path between each pair of vertices A directed graph is strongly connected if there is

a path between each pair of vertices A directed graph is weakly connected if the

underlying undirected graph is connected A complete graph is a graph in which there is an

edge between every pair of vertices

Page 6: DATA STRUCTURES  AND ALGORITHMS

6

GRAPHSExample : Airport system Each airport is a vertex Two vertices are connected by an egde if there

is a nonstop flight from the airports that are represented by the vertices

The edge could have a weight, representing time, distance or cost of flight.

Such a graph is directed It might have longer or cost more to fly in different

directions Make sure that the airport system is strongly

connected It is always possible to fly from any airport to any

other

Page 7: DATA STRUCTURES  AND ALGORITHMS

7

GRAPHS Traffic flow can be modeled by a

graph Each street intersection represents a

vertex, each street is an edge. Edge costs could represent, among

other things, a speed limit or capacity We could ask for the shortest route or

use this information to find the most likely location for bottleneck

Page 8: DATA STRUCTURES  AND ALGORITHMS

8

Representation of Graphs We will consider directed graphs We number the vertices, starting at 1. The graph below represents 7 vertices and 12 edges

Page 9: DATA STRUCTURES  AND ALGORITHMS

9

Representation of Graphs Adjacency Matrix Representation

Use a two dimensional array to represent a graph

For each edge (u,v) Є E we set A [u][v] = 1 A [u][v] = 0 , otherwise

If the edge has a weight we set A[u][v] = weightwe can use -∞ / ∞ to indicate nonexistent edges

Space requirement = θ(|v|2) An adjacency matrix is an appropriate

representation if the graph is dense: |E| = θ (|v|2) is it true in most applications ?

Page 10: DATA STRUCTURES  AND ALGORITHMS

10

Representation of Graphs

Adjacency List Representation If the graph is not dense (is sparse) a

better solution is adjacency list representation

For each vertex, we keep a list of all adjacent vertices.

The space requirement is O(|E|+|V|)

Page 11: DATA STRUCTURES  AND ALGORITHMS

11

Representation of Graphs

Adjacency list representation of a graph

Page 12: DATA STRUCTURES  AND ALGORITHMS

12

Topological Sort

A topological sort is an ordering of vertices in a directed acyclic graph If there is a path vi to vj, vj appears

after vi in the ordering

Example : course prerequisite structure

Page 13: DATA STRUCTURES  AND ALGORITHMS

13

Topological Sort

course prerequisite example

Page 14: DATA STRUCTURES  AND ALGORITHMS

14

Topological Sort If a graph has a cycle, a topological sort is not possible Ordering is not necessarily unique, any legal ordering will do On the example below, v1, v2, v5, v4, v3, v7, v6 and

v1, v2, v5, v4, v7, v3, v6 are both topological orderings

Page 15: DATA STRUCTURES  AND ALGORITHMS

15

Topological Sort

AlgorithmRepeat

find a vertex with no incoming edgesprint the noderemove it and its edges

Until the graph is empty

How to find a vertex with no coming edges ?

Page 16: DATA STRUCTURES  AND ALGORITHMS

16

Topological Sort

To find a vertex with no coming edges Linear search on vertices

O(|V|) for each find O(|V|2) for topological sort

Keep a list (queue or stack) for vertices with zero incoming edges Update the list when an edge is deleted

O(|E|) if adjacency list is used. Total time

O(|E|+|V|)

Page 17: DATA STRUCTURES  AND ALGORITHMS

17

/*Simple topological sort pseudocode void Graph::topsort(){

Vertex v, w;

for (int counter = 0; counter < NUM_VERTICES; counter ++){

v = findNewVertexOfDegreeZero ();if ( v==NOT_A_VERTEX )

throw CycleFound ();v.TopNum = counter ; for each w adjacent to v

w.indegree-- ;}

}

Page 18: DATA STRUCTURES  AND ALGORITHMS

18

Result of Applying topological sort to the graph below

Page 19: DATA STRUCTURES  AND ALGORITHMS

19

/* Pseudocode to perform topological sort void Graph::topsort() {

Queue q(NUM_VERTICES);int counter = 0;Vertex v, w;

q.makeEmpty();for each vertex v

if (v.indegree == 0)q.enqueue(v);

while (!q.isEmpty()){

v = q.dequeue();v.topNum = ++counter; // assign next number

for each w adjacent to vif (--w.indegree == 0)

q.enqueue (w); }

if (counter !=NUM_VERTICES)throw CycleFound ();

}