Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Graphs

Advanced Algorithms

03/01/22 Advanced Algorithms 1


Graphs
• Graph background and terminology
• Data structures for graphs
• Graph traversal algorithms
• Minimum spanning tree algorithm
• Shortest-path algorithm
• Asymptotic Notations

Advanced Algorithms
Graph Terminology
• A graph is an ordered pair G=(V,E) with a set of
vertices or nodes and the edges that connect them

• A subgraph of a graph has a subset of the vertices


and edges
• Graph G = (V, E)
– V = set of vertices
– E = set of edges  (VV)

03/01/22 Advanced Algorithms


Graph Terminology..
• Types of graphs
– Undirected: edge (u, v) = (v, u); for all v, (v, v)  E (No self
loops.)
– Directed: (u, v) is edge from u to v, denoted as u  v. Self
loops are allowed.
– Weighted: each edge has an associated weight
Graph Terminology…
• A path is a subset of E that is a series of edges
between two nodes

• A graph is connected if there is at least one


path between every pair of nodes

• The length of a path in a graph is the number


of edges in the path
Graph Terminology…
• A cycle is a path that begins and ends at the same node
• An acyclic graph is one that has no cycles
• An acyclic, connected graph is also called an unrooted
tree
• A complete graph is one that has an edge between
every pair of nodes
Data Structures for Graphs
an Adjacency Matrix
• A two-dimensional matrix or array that has one
row and one column for each node in the graph

• For each edge of the graph (Vi, Vj), the location of


the matrix at row i and column j is 1

• All other locations are 0

7
Data Structures for Graphs
An Adjacency Matrix
• For an undirected graph, the matrix will be
symmetric along the diagonal

• For a weighted graph, the adjacency matrix would


have the weight for edges in the graph,
zeros along the diagonal, else infinity (∞) .

8
Adjacency Matrix
• |V|  |V| matrix A.
• Number vertices from 1 to |V| in some arbitrary
manner.
1 2
a b 1 2 3 4
1 0 1 1 1
2 0 0 1 0 1 if (i, j )  E
A[i, j ] aij 
c d4 3 0 0 0 1 0 otherwise
3 4 0 0 0 0

1 2 1 2 3 4
a b
1 0 1 1 1
A = AT for undirected graphs.
2 1 0 1 0
c d 3 1 1 0 1
3 4 4 1 0 1 0
Space and Time
• Space: (V2).
– Not memory efficient for large graphs.
• Time: to list all vertices adjacent to u: (V).
• Time: to determine if (u, v)  E: (1).
• Can store weights instead of bits for weighted graph.
Adjacency Matrix Example 1

11
Adjacency Matrix Example 2

12
Data Structures for Graphs
An Adjacency List

• A list of pointers, one for each node of the graph

• These pointers are the start of a linked list of


nodes that can be reached by one edge of the
graph

• For a weighted graph, this list would also include


the weight for each edge

13
Adjacency List Example 1

14
Adjacency List Example 2

15
Breadth-First Traversal
• From the starting node, follow all paths of
length one

• Then follow paths of length two that are


unvisited.

• Continue increasing the length of the paths until


there are no unvisited nodes along any of the
paths

16
Breadth-First Traversal Example

• Consider the following graph:

• The order of the breadth-first traversal of this graph


starting at node 1 would be: 1, 2, 8, 3, 7, 4, 5, 9, 6

17
Depth-First Traversal
• Follow a path through the graph until it reaches
a dead end

• Then back up until it reaches a node with an


edge to an unvisited node

• Repeat it reaches a dead end

• This process continues until it reaches to the


starting node and it has no edges to unvisited
nodes

18
Depth-First Traversal Example
• Consider the following graph:

• The order of the depth-first traversal of this graph


starting at node 1 would be:
1, 2, 3, 4, 7, 5, 6, 8, 9

19
Data Structures use in DFS
a b e

b d c
V a d
Graph with 5 Vertices
F b a c e

U c d b e

F d a e c TOS
b
U e b d c d

(U/V/F) Adjacency list Fringe


1-D Array
(Stack)
Analysis of DFS
For a Graph G=(V, E) and n = |V| & m=|E|
 When Adjacency List is used
 Complexity is O(m+n)
 When Adjacency Matrix is used
 This again requires a stack to maintain the fringe & an array for
maintaining the state of a node.
 Scanning each row for checking the connectivity of a Vertex is in order
O(n).
 So, Complexity is O(n2).
Applications of DFS
 To check if a graph is connected. The algorithm ends when the Stack
becomes empty.
• Graph is CONNECTED if all the vertices are visited.
• Graph is DISCONNECTED if one or more vertices
remained unvisited.

 Finding the connected components of a disconnected graph.

 Detecting if a given directed graph is strongly connected


Analysis of BFS

• Fringe part is maintained in a Queue.


• The trees developed are more branched & wider.
 When Adjacency List is used
 Complexity is O(m+n)
 When Adjacency Matrix is used
 This requires a Queue to maintain the fringe & an array for maintaining the
state of a node.
 Scanning each row for checking the connectivity of a Vertex is in order O(n).
 So, Complexity is O(n2).
DFS vs. BFS
DFS Process F B A start
E

G D C
destination

C DFS on C D Call DFS on D


B DFS on B B B Return to call on B
A DFS on A A A A

G Call DFS on G found destination - done!


Path is implicitly stored in DFS recursion
D Path is: A, B, D, G
B
A
DFS vs. BFS
F B A start
E
BFS Process
G D C
destination

rear front rear front rear front rear front

A B D C D
Initial call to BFS on A Dequeue A Dequeue B Dequeue C
Add A to queue Add B Add C, D Nothing to add
rear front

G found destination - done!


Path must be stored separately
Dequeue D
Add G
Minimum Spanning Tree
• A spanning tree of an undirected graph G is a
subgraph of G that is a tree containing all the
vertices of G.
• In a weighted graph, the weight of a subgraph
is the sum of the weights of the edges in the
subgraph.
• A minimum spanning tree (MST) for a
weighted undirected graph is a spanning tree
with minimum weight.
Minimum Spanning Tree

An undirected graph and its minimum spanning tree.


Minimum Spanning Tree: Prim's
Algorithm
• Prim's algorithm for finding an MST is a greedy
algorithm.
• Start by selecting an arbitrary vertex, include it
into the current MST.
• Grow the current MST by inserting into it the
vertex closest to one of the vertices already in
current MST.
Minimum Spanning Tree: Prim's Algorithm

Prim's minimum spanning tree algorithm.


Minimum Spanning Tree (MST)
• A spanning tree of a connected Graph G is a
sub-graph of G which covers all the vertices of G.

• A minimum-cost spanning tree is one whose edge weights


add up to the least among all the spanning trees.

• A given graph may have more than one spanning tree.

• DFS & BFS give rise to spanning trees, but they don’t consider
weights.
Kruskal’s Algorithm
• An algorithm to find the minimum spanning tree of connected
graph.

Step 1: Initially all the edges of the graph are sorted based on
their weights.

Step2: Select the edge with minimum weight from the sorted list
in step 1.Selected edge shouldn’t form a cycle. Selected edge
is added into the tree or forest.

Step 3: Repeat step 2 till the tree contains all nodes of the graph.
Asymptotic Notation
• O notation: asymptotic “less than”:

f(n)=O(g(n)) implies: f(n) “≤” g(n)

•  notation: asymptotic “greater than”:

– f(n)=  (g(n)) implies: f(n) “≥” g(n)

•  notation: asymptotic “equality”:

– f(n)=  (g(n)) implies: f(n) “=” g(n)

32
Asymptotic notations
• O-notation

33
Asymptotic notations (cont.)
•  - notation

(g(n)) is the set of functions with


larger or same order of growth
as g(n)

34
The Master Theorem
• Given: a divide and conquer algorithm
– An algorithm that divides the problem of size n
into a subproblems, each of size n/b
– Let the cost of each stage (i.e., the work to divide
the problem + combine solved subproblems) be
described by the function f(n)
The Master Theorem
• if T(n) = aT(n/b) + f(n) then
 

  
n 
logb a
 
f (n) O n logb a   

 
   0
T (n)  n  logb a
log n  f (n)  n 
logb a

  c 1
 
 f (n)   
f (n)  n logb a  AND 
 
 af (n / b)  cf (n) for large n
Using The Master Method
• T(n) = 9T(n/3) + n
– a=9, b=3, f(n) = n
– nlog a = nlog 9 = (n2)
b 3

– Since f(n) = O(nlog 9 - ), where =1, case 1 applies:


3

  
T ( n)  n logb a when f ( n) O n logb a  
– Thus the solution is T(n) = (n )2

Thank You

You might also like