Docsity

CS 331 Wk 15: Graph Algorithms - BFS, DFS, Topological Sort, SCC, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS 331 - Week 15 - Page 1
______________________________________________________________________________
CS 331
Week 15
______________________________________________________________________________
Reading:
1. Introduction of Algorithms by Cormen, Chapter 22
Objectives:
1. To learn about various graph algorithms.
2. To get good understanding of the various applications of graph algorithms.
Concepts:
1. Introduction
2. Graph Search Algorithms
3. Topological Sort
4. Strongly Connected Components
5. Lab
Outline:
1. Introduction
2. Graph Search Algorithms
a) Breadth First Search
b) Depth First Search
3. Topological Sort
a) Definition
b) Algorithm
c) Properties
4. Strongly Connected Components
a) Definition
b) Algorithm
c) Properties
5. Lab
Reference:
1. www.fmt.cs.utwente.nl/courses/adc/lec6.pdf
2. www.icl.pku.edu.cn/yujs/papers/pdf/ComAlg11.pdf
3. http://www.cs.fsu.edu/~cop4531/slideshow/chapter23/toc.html
pf3
pf4
pf5

Partial preview of the text

Download CS 331 Wk 15: Graph Algorithms - BFS, DFS, Topological Sort, SCC and more Study notes Data Structures and Algorithms in PDF only on Docsity!

______________________________________________________________________________

CS 331

Week 15 ______________________________________________________________________________

Reading:

  1. Introduction of Algorithms by Cormen, Chapter 22

Objectives:

  1. To learn about various graph algorithms.
  2. To get good understanding of the various applications of graph algorithms.

Concepts:

  1. Introduction
  2. Graph Search Algorithms
  3. Topological Sort
  4. Strongly Connected Components
  5. Lab

Outline:

  1. Introduction
  2. Graph Search Algorithms a) Breadth First Search b) Depth First Search
  3. Topological Sort a) Definition b) Algorithm c) Properties
  4. Strongly Connected Components a) Definition b) Algorithm c) Properties
  5. Lab

Reference:

  1. www.fmt.cs.utwente.nl/courses/adc/lec6.pdf
  2. www.icl.pku.edu.cn/yujs/papers/pdf/ComAlg11.pdf
  3. http://www.cs.fsu.edu/~cop4531/slideshow/chapter23/toc.html
  1. Introduction

Graphs:

a) G(V,E) — V : vertex set; E: edge set. b) Directed graphs, undirected graphs, weighted graphs. c) A path from a vertex v to vertex v’ is a sequence of vertices (v 0 , v 1 , …., vk) such that v = v 0 , v’ = vk , and (vi-1, vi) belongs to E for i = 1, 2, ….., k. d) Length of path: either the number of edges in the path or the total weight. e) Simple path: all vertices in the path are distinct. f) Cyclic graph: graph that consist of a cycle. g) Acyclic graph: graph that contains no cycle. h) Graph Representation:

  • Adjacency Matrix
  • Adjacency List
  1. Graph Searching Algorithms

Breadth First Search

a) Definition: i. Simplest graph search algorithm ii. Begins at the root node and explores all the neighboring nodes iii. For those nearest nodes, it explores their unexplored neighbor nodes and so on until it finds a goal.

b) Basic Strategy:

i. Mark the current vertex v as “discovered” ii. For each edge (v, w) in graph G with “undiscovered” successor w

  • continue searching simultaneously from all such w
  • no backtracking iii. Mark vertex v as “finished”. iv. This yields the set of vertices that are reachable from start vertex.

c) Properties of BFS

i. Vertices are visited in order of increasing distance from the start vertex

  • after processing all vertices at distance d , all vertices at distance d+1 are processed
  • search terminates when at distance d no vertices occur. ii. Space complexity: Ө(|V|) iii. Time Complexity: Ө(|V| + |E|) iv. Vertices to be processed are organized in First-in-First-out queue.

CS 331: Week 15 - Lecture Notes

  1. Topological Sort

a) Definition: i. A topological sort of Directed Acyclic Graph (DAG) is a linear ordering of all its vertices such that,

  • If G contains an edge (u,v) then u appears before v in the ordering. ii. If a digraph has a cycle, then G has no topological order. iii. Each DAG has at least one topological order.

b) Algorithm:

i. Use DFS(G) to compute finishing time f(v) for each vertex v ii. As each vertex is finished, insert it into the front of the linked list iii. Return the linked list of vertices.

c) Properties

i. Insertion into the linked list cost Ө (1) for each |V| insertions. ii. The time cost of topological sort is only Ө(|V| + |E|)

d) Application:

i. Decomposing a directed graph into its strongly connected components.

  1. Strongly Connected Components (SSC)

a) Definition:

i. A digraph is called strongly connected if for every pair of vertices u and v , there is a path from u to v and a path from v to u. ii. The Strongly Connected Components (SSC) of a digraph are its maximal strongly connected subgraphs. iii. If each strongly connected component is contracted to a single vertex, the resulting graph is a directed acyclic graph.

b) Algorithm:

i. Perform DFS(G) to compute finishing time f(u) for each vertex u ii. Compute transposed graph GT iii. Perform DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u] iv. Produce as output the vertices of each tree in the DFS forest as a separate SCC.

c) Properties:

i. G and GT^ have exactly the same strongly connected components. ii. Time complexity is Ө(|V| + |E|):

  • Each DFS takes Ө(|V| + |E|)
  • Constructing the transposed graph takes Ө(|V| + |E|)
  1. Lab

a) Implementing BFS algorithm and analyzing its time complexity. b) Implementing DFS algorithm and analyzing its time complexity. c) Implementing Topological Sort to compute Strongly Connected Components.