Algorithm Analysis (CSC 301 2015F) : EBoards

CSC301.01 2015F, Class 25: Implementing the Graph ADT


Overview

Preliminaries

Admin

Upcoming Work

Extra Credit

Academic

Peer Support

Questions

Traversal algorithms

Breadth-First Search

define bfs(Vertex v) { Queue q = new Queue(); q.enqueue(v); mark(v,"discovered"); while (!q.isEmpty()) { c = q.dequeue(); for each n in c.neighbors() { if (! (marked(n, "processed") || marked(n, "discovered"))) { mark(n, "discovered"); q.enqueue(n); } } mark(c, "processed"); } // while }

define bfs(Vertex v) { bfsKernel(v, new Queue()) }

define bfs(Vertex v, Queue q) { mark(v, "visited"); for each n in v.neighbors() { if (! marked(n, "visited")) { q.enqueue(n); } } // for bfs(q.dequeue(), q); } // bfs

Depth-First Search

Key operations

Breadth-First Search (Trees vs. Graphs)

Depth-First Search (Trees vs. Graphs)

Marking vertices and edges

Three graph implementations and their costs

What is the cost of the key operations for each of these representations?

Group 1: Edge list Group 2: Neighbor list Group 3: Adjacency Matrix Group 4: Neighbor list Group 5: Adjacency Matrix

In terms of n - number of vertices, m - number of edges

Edge list

Neighbor list

Adjacency matrix