Download presentation
Presentation is loading. Please wait.
1
Data Structures, Algorithms & Complexity
GRIFFITH COLLEGE DUBLIN Data Structures, Algorithms & Complexity Elementary Graph Algorithms Lecture 16
2
A Graph A graph can be thought of a collection of vertices (V) and edges (E), so we write, G = (V, E) Graphs can be directed, or undirected, weighted or unweighted. A directed graph, or digraph, is a graph where the edge set is an ordered pair. That is, edge 1 being connected to edge 2 does not imply that edge 2 is connected to edge 1. An undirected graph is a graph where the edge set in an unordered pair. That is, edge 1 being connected to edge 2 does imply that edge 2 is connected to edge 1. Lecture 16
3
A Weighted Graph A weighted graph is graph which has a value associated with each edge. This can be a distance, or cost, or some other numeric value associated with the edge. (a) A directed graph (b) An undirected graph (c) A weighted graph 1 2 3 4 5 6 2 5 2 1 4 3 Lecture 16
4
Representing a Graph There are two standard ways to represent a graph G = (V,E) in computer science As a collection of adjacency lists As an adjacency matrix Take the following graph 1 2 3 4 5 We can represent this in two ways Lecture 16
5
Adjacency Lists & matrices
As an adjacency list this is As an adjacency matrix this is Lecture 16
6
Adjacency Matrix For a directed graph such as 1 2 3 4 5 6
The adjacency matrix is Lecture 16
7
Adjacency List And the adjacency list is
The adjacency list representation is usually preferred, because it provides a compact way to represent sparse graphs A sparse graph is defined as one for which |E| is much less that |V| squared Sparse graphs are very common in real-world situations Lecture 16
8
Graphs in the Real World
Take the snippet of a map shown We can easily represent this in diagrammatic form Suppose we know the distances between the towns Lecture 16
9
Graph Assuming A = Lemybrien, B = Kilmacthomas, C = Newtown
D = Kilmeadan, E = Dunhill, F = Tramore G = Butlerstown, and H = Waterford 6 3 1 C D G 3 B 5 8 H 5 12 10 4 A E F Lecture 16
10
Representing Graphs An adjacency matrix representation may be preferred, however, when the graph is dense, or when we need to be able to tell quickly if there is an edge connecting two given vertices If a graph is a weighted graph, then the weight can be stored with the vertex in the adjacency list In Java we can do this using a class Node To store the weight in an adjacency matrix, simply store the weight instead of a 1 in the entry for vertex u and vertex v Lecture 16
11
Searching a Graph Breadth-First-Search (BFS) is a search algorithm which forms the basis for many important graph algorithms Given a graph G = (V, E) and a source vertex s, breadth-first search systematically explores the edges of G to discover every vertex that is reachable from s It also produces a “breadth-first-tree” with root s that contains all such 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 fewest number of edges The algorithm works on both directed and undirected graphs Lecture 16
12
Breadth First Search The algorithm is called Breadth-First-Search because it discovers all vertices at distance k from s before discovering any vertices at distance k+1 Using the BFS we speak of colouring the nodes as they are discovered. A white node has not yet been discovered. A grey node has been discovered but may have some adjacent white vertices. All adjacent vertices of a black node have been discovered The algorithm we are going to look at assumes the graph is represented as an adjacency list The algorithm also uses a queue to manage the set of grey vertices Lecture 16
13
BFS Algorithm The predecessor of u is stored in p[u], the distance from the source s to vertex u is stored in the array d[u] BFS(G, s) for each vertex u V(G) - {s} colour[u] = white, d[u] = endfor colour[s] = grey, d[s] = 0, Q = {s} while Q <> 0 u = head[Q] for each v Adj[u] if colour[v] = white then colour[v] = grey, d[v] = d[u] + 1, Enqueue(Q, v) endif Dequeue(Q) colour[u] = black endwhile endalg Lecture 16
14
BFS Example Q s (0) Q w(1) r(1) Q r(1) t(2)
u r s t u Q s (0) 1 Q w(1) r(1) 1 v w x y v w x y r s t u r s t u 1 2 Q r(1) t(2) x(2) 1 2 Q t(2) x(2) v(2) 1 2 2 1 2 v w x y v w x y r s t u r s t u 1 2 3 Q x(2) v(2) u(3) 1 2 3 Q v(2) u(3) y(3) 2 1 2 2 1 2 3 v w x y v w x y r s t u r s t u 1 2 3 Q u(3) y(3) 1 2 3 Q y(3) 2 1 2 3 2 1 2 3 v w x y v w x y r s t u 1 2 3 Q 2 1 2 3 v w x y
15
Analysis of BFS on G = (V,E)
After initialisation, no vertex is ever whitened, so each vertex is enqueued only once, and hence dequeued only once. The operations of enqueueing and dequeueing take O(1) time, so the total time devoted to queue operations is O(V) The adjacency list of each vertex is scanned only when the vertex is dequeued, the adjacency list of each vertex is scanned at most once. Since the sum of the lengths of all the adjacency lists is E, at most O(E) time is spent in scanning the adjacency lists. The total running time of BFS is O(V + E) Breadth First Search runs in time linear in the size of the adjacency list G Lecture 16
16
Depth First Search DFS also runs in O(V+E) time. Instead of using a queue, however, the DFS can use a stack or can be implemented recursively DFS(G, s) for each vertex u V[G] - {s} colour(u) = white, d[u] = endfor S = {s}, d[s] = 0 while S <> 0 v = pop(S), colour(v) = black for each u adj[v] if colour(u) = white then colour(u) = grey, d[u] = d[v] + 1, push(S, u) endif endwhile endalg Lecture 16
17
DFS Example S s (0) S r(1) w(1) S v(2)
t u r s t u S s (0) 1 S r(1) w(1) 1 v w x y v w x y r s t u r s t u 1 S v(2) w(1) 1 S w(1) 2 1 2 1 v w x y v w x y r s t u r s t u 1 2 S t(1) x(2) 1 2 3 S u(3) x(2) 2 1 2 2 1 2 v w x y v w x y r s t u r s t u 1 2 3 S y(4) x(3) 1 2 3 S x(3) 2 1 2 4 2 1 2 4 v w x y v w x y r s t u 1 2 3 S 2 1 2 4 v w x y
18
Summary Graphs can be directed, or undirected, weighted or unweighted
G = (V, E) can be represented As a collection of adjacency lists As an adjacency matrix Two methods of searching Breadth First Search Depth First Search The analogy used that BFS is like an army of searchers fanning out DFS is like a single searcher probing as deeply as possible, retreating only when hitting dead ends. BFS finds shortest paths, DFS doesn’t Lecture 16
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.