This document provides lecture notes on elementary graph algorithms, including definitions of graphs and their representations using adjacency matrices and lists. It describes two common graph search algorithms: breadth-first search (BFS) and depth-first search (DFS). BFS systematically explores the edges of a graph to discover all vertices reachable from a source vertex s, computing the shortest distances and producing a breadth-first tree. DFS gives priority to exploring longer paths first, using time stamps to track the discovery and finishing of nodes. Both algorithms have a time complexity of O(V+E) where V is the number of vertices and E is the number of edges.
This document provides lecture notes on elementary graph algorithms, including definitions of graphs and their representations using adjacency matrices and lists. It describes two common graph search algorithms: breadth-first search (BFS) and depth-first search (DFS). BFS systematically explores the edges of a graph to discover all vertices reachable from a source vertex s, computing the shortest distances and producing a breadth-first tree. DFS gives priority to exploring longer paths first, using time stamps to track the discovery and finishing of nodes. Both algorithms have a time complexity of O(V+E) where V is the number of vertices and E is the number of edges.
Fall 2008 Elementary Graph Algorithms by Rahul Gupta (Instructor: Predrag Radivojac)
Elementary Graph Algorithm
A graph G can be defined as a set of vertices V and edges. G =(V,E) Graphs can be represented as (1) Adjacency matrices (2) Adjacency lists
Panel (a) in the following diagram shows an undirected graph G with 5 vertices and 7 edges. Panel (b) and (c) demonstrate an adjacency-list and adjacency matrix representation for this graph respectively.
Graphs can be searched using one of the following two methods: (1) Breadth First Search (2) Depth First Search
Breadth First Search (BFS) Breadth-first search is one of the simplest algorithms for searching a graph and the archetype for many important graph algorithms. Prim's minimum-spanning-tree algorithm and Dijkstra's single-source shortest-paths algorithm use ideas similar to those in breadth-first search. Given a graph G =(V, E) and a distinguished source vertex s, breadth-first search systematically explores the edges of G to "discover" every vertex that is reachable from s. It computes the distance (smallest number of edges) from s to each reachable vertex. It also produces a "breadth- first tree" with root s that contains all reachable vertices. For any vertex v reachable from s, the path in the breadth-first tree from s to v corresponds to a "shortest path" from s to v in G, that is, a path containing the smallest number of edges. The algorithm works on both directed and undirected graphs.
Breadth-first search is so named because it expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. That is, the algorithm discovers all vertices at distance k from s before discovering any vertices at distance k +1.
Other Method : Circle Colouring (or labeling from class notes)
d u u u u e d d u u abcde abcde a e d b c 3.Expand b => b bcomes e & c becomes d
4. Expand c => c bcomes e & d becomes d
5. Expand d => d bcomes e e becomes d
6.Expand e => e bcomes e Q is empty now.
Note : BFS is used in Speech recognition programs after pruning.
How to store this tree : Parent [ ] How to store colours : colours [ ] Complexity of BFS e e d d u e e e d u e e e e d abcde abcde abcde a b c e d Enqueing/Dequeuing is O(1) ( |V| ) to enqueue/dequeue node O( |E| ) to find neighbors
Complexity : O ( | V | + | E | )
Properties of BFS : Can find all reachable nodes from start node. Provide shortest distance from start node to all reachable nodes.
Deapth First Search (DFS) Gives priority to longer nodes
Only when vs children are explored,we can explore vs sibling We will time stamp: discovery of node When a node is finished
Pseudocode For all u v Colour[u]=u Parent [u] =Nil u z v Time <- 0 For all u v If colour[u]=u DFS_VISIT(u) time<-time+1 colour[u]=d discover[u]<-time for every v reachable from u if colour[v]=u parent[v] <- u DFS_VISIT(v) Colour [u] <-e Time<-time+1 Finish[u] <- time
Complexity of DFS O( |V| ) to enqueue/dequeue node O( |E| ) for DFS_VISIT Complexity : O ( | V | + | E | )
Reference: Panel (a ,b,c) : Fig 22.1, Pg 528 Introduction to Algorithms, 2nd ed. - by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, MIT Press, 2001
Tshri Vile Parle Kelvani Mandal'S D. J. Sanghvi College of Engineering Department of Information Technology Semester: Iii Subject: Java Programming Lab
Tshri Vile Parle Kelvani Mandal'S D. J. Sanghvi College of Engineering Department of Information Technology Semester: Iii Subject: Java Programming Lab