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
ChatGPT Side Hustles 2024 - Unlock the Digital Goldmine and Get AI Working for You Fast with More Than 85 Side Hustle Ideas to Boost Passive Income, Create New Cash Flow, and Get Ahead of the Curve
ChatGPT Millionaire 2024 - Bot-Driven Side Hustles, Prompt Engineering Shortcut Secrets, and Automated Income Streams that Print Money While You Sleep. The Ultimate Beginner’s Guide for AI Business
ChatGPT Money Machine 2024 - The Ultimate Chatbot Cheat Sheet to Go From Clueless Noob to Prompt Prodigy Fast! Complete AI Beginner’s Course to Catch the GPT Gold Rush Before It Leaves You Behind