12 Graphs Bfs
12 Graphs Bfs
Graphs
Introduction and BFS
Yusuf Osmanlioglu
Department of Computer Science
Drexel University
Graphs
2
Graphs
Amsterdam Berlin
690
● Graphs provide an abstract model for
205 representing relationships between objects.
380 ● Basic Components of a Graph
Brussels
○ Vertex: An object. Also referred to as Nodes or
Frankfurt 530 Points
405
Paris 290 ○ Edge: A relationship between two objects. Also
Prague called arcs.
585
360 ● Edges can be directed/undirected
550
○ Directed: One way relationship
Munich ○ Undirected: Two way relationship
550
Geneva 410 ● Edges can be weighted/unweighted
Milan 650
○ Weighted: Quality of relationship differs
310
across the nodes
390
Marseille 250 ○ Unweighted: Quality of the relationship is
Florence standard among nodes
3
Graphs: Directed/Undirected
● A directed graph has edges represented as lines with arrows
○ You can travel from A to B but not B to A
A B
A B
4
Graphs: Formal definition
● A graph G =(V,E) is a pair of sets: Amsterdam
690
Berlin
○ V: vertex set.
205
○ V={Amsterdam, Berlin, Brussels, 380
Prague
○ E: edge set. 360
585
550
○ E={(Amsterdam,Berlin), (Amsterdam, Munich
550
Brussels), (Brussels, Frankfurt),…, Geneva 410
Milan 650
(Munich, Florence)}
310
Florence
5
Node and edge values
● Nodes and Edges can be given values. (called “annotated graphs”)
● Nodes:
○ People (Names)
○ Places (Name, GPS location, address)
○ Items (Price)
● Edges
○ Directions (distance)
○ Time to complete task
○ Cost (Price, Gas, Electricity)
● We will mostly use distance on edges.
6
Graphs: further definitions
● Path Amsterdam
690
Berlin
● Cycle 405
Frankfurt 530
Prague
vertex 360
585
● Sub-graph 550
Munich
○ A subgraph of a graph G is another 550
Geneva 410
graph formed from a subset of the Milan 650
390
Marseille 250
● Degree of a node Florence
○ The degree of a vertex in a graph is its
number of incident edges.
7
Graphs: notations
● Given A graph G=(V,E), where
○ V is its vertex set, |V|=n,
○ E is its edge set, with |E|=m=O(n2).
● If G is connected then for every pair of vertices u,v in G, there is path connecting them.
● In an undirected graph, edges (u,v)=(v,u).
● In a directed graph, edges (u,v)≠(v,u).
● In a weighted graph there are weights associated with edges and/or vertices.
● Running time of graph algorithms are usually expressed in terms of n or m.
8
Graphs: Sample use cases
For the following use cases, determine what would nodes and
edges represent, whether the graphs would be
directed/undirected, and weighted/unweighted?
● Flight scheduling
○ Cities as nodes and prices of flights (or distance) as edges,
weighted, directed
● Operating systems
○ Programs and resources as nodes, and their dependency as edges,
unweighted, directed
● City planning for natural gas pipeline infrastructure
○ Houses as nodes and the digable roads between houses (along
with their distance) as edges, weighted, undirected
● Communication in brain
○ Brain regions as nodes and the structural connectivity between
brain regions as edges, undirected, weighted
9
Graph representations
● Basic Graphs:
○ Nodes, Edges, Edge Weights
● The weight of an edge is its value
● These can be easily extended to have node weights
● Two Main Methods to represent graphs:
○ Adjacency List
○ Adjacency Matrix
10
Graph Representations: Adjacency Matrix
A
● The adjacency matrix of a graph G, denoted by AG is G
an n x n matrix defined as follows:
B D
11
Graph Representations: Adjacency Matrix
A
● Given a graph G=(V,E), where |V|=n,|E|=m: G
● If G is directed, then number of 1’s in AG is
○ m B D
● If G is undirected, then number of 1’s in AG is
○ 2m
C
● Degree of a vertex is
○ the sum of entries in corresponding row of AG
● If G is undirected then sum of all degree is
○ 2m
● In G is directed, sum of the out degrees is equal to
○ m
12
Graph Representations: Adjacency List
● For each vertex v in V, a list Adj[v] will represent vertices that are adjacent to v.
● Size of this list is the degree of v.
Node
A B C
G A
C
B D B
C D C
13
Graph Representations: Pros / Cons
● Matrix Advantages:
○ Fixed Memory Usage (No Pointer Management)
○ Constant Time Edge Lookups
● Matrix Disadvantages:
○ Wasted Memory. (Always uses Θ(n2) memory.)
● List Advantages:
○ Memory Optimized (Best Case Θ(n), Worst Case Θ(n2))
● List Disadvantages:
○ Θ(n) edge lookups
○ Worst case memory is still Θ(n2)
● Trade-off: Space (Lists) vs Speed (Matrix)
14
Searching Graphs
● How do you find your way through a maze?
15
Searching Graphs
● There are two major graph traversal algorithms:
○ Breadth First search
○ Depth First Search
A
Breadth First Search
B C Depth First Search
D E F G
H I J K
L M N O P Q
16
Breadth First Search: Example
r s t u r s t u r s t u r s t u
∞ 0 ∞ ∞ 1 0 ∞ ∞ 1 0 2 ∞ 1 0 2 ∞
∞ ∞ ∞ ∞ ∞ 1 ∞ ∞ ∞ 1 2 ∞ 2 1 2 ∞
v w x y v w x y v w x y v w x y
Q: s Q: w r Q: r t x Q: t x v
r s t u r s t u r s t u r s t u
1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3
2 1 2 3 2 1 2 3 2 1 2 3 2 1 2 ∞
v w x y v w x y v w x y v w x y
Q: y Q: u y Q: v u y Q: x v u
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: 17
Breadth First Search (BFS)
● Given a graph G = (V,E), BFS starts at some source vertex s and
r s t u
discovers which vertices are reachable from s. 1 0 ∞ ∞
● Distance between a vertex v and s:
○ is the minimum number of edges on a path from s to v. ∞ 1 ∞ ∞
v w x y
● BFS discovers vertices in increasing order of distance
Q: w r
○ Thus, it can be used as an algorithm for computing shortest paths.
● At any given time there is a frontier of vertices that have been
discovered, but not yet processed.
○ BFS is so named because it visits vertices across the entire breadth of this
frontier.
18
BFS: coloring and arrays
● We use the following coloring procedure to show the status of BFS
at each instance of time: r s t u
1 0 ∞ ∞
○ Initially all vertices (except the source) are colored white,
■ meaning that they are undiscovered. ∞ 1 ∞ ∞
○ When a vertex has first been discovered, it is colored gray v w x y
■ and is part of the frontier Q: w r
○ When a gray vertex is processed, it becomes black.
● We also maintain arrays
○ color[u] which holds the color of vertex u
■ either white, gray or black
○ pred[u] which points to the predecessor (or parent) of u
■ i.e. the vertex who first discovered u
○ dist[u], the distance from s to u.
19
r s t u
BFS: queue and BFS tree 1 0 ∞ ∞
∞ 1 ∞ ∞
v w x y
● The search makes use of a queue, a first-in-first-out list, where elements Q: w r
are removed in the same order as they are inserted.
r s t u
● Observe that the predecessor pointers of the BFS search define an 1 0 2 3
● These edges of G are called tree edges and the remaining edges of G are s 0
21
BFS: Runtime ● Initialization requires Θ(V) time.
● Since every vertex will be visited only
BFS(G,s) once, the number of times we go
1 for each u in V
2 color[u] = white;
Θ(V)
through the while loop is Θ(V).
3 dist[u] = INFINITY;
4 pred[u] = NULL; ● The number of iterations through the
5 color[s] = gray; inner for loop
6 dist[s] = 0; Θ(1)
7 Q = {s}; ○ is proportional to degree(u).
8 while (Q is nonempty) Θ(V) ○ summing up over all vertices we get:
9 u = Dequeue[Q]; Θ(V)
10 for each v in Adj[u]
11 if (color[v] == white) Θ(V+E)
12 color[v] = gray;
13 dist[v] = dist[u]+1; Θ(V+E) Why 2?
14 pred[v] = u; Θ(V)
15 Enqueue(Q, v);
16 color[u] = black; Θ(V) ● Overall runtime:
○ Θ(V)+Θ(1)+Θ(V)+Θ(V)+Θ(V+E)+Θ(V)
22