Title: Graph Algorithms
1Graph Algorithms
By Shruti Aggrawal, Preeti M. Palkar
2Table Of Contents
- Graphs
- Eulers Circuit Problem
- Graph Terminologies (Paths, Cycles, Subgraph,
Connectivity, Spanning Tree, Forest) - Depth First Search
- Breadth First Search
- Minimum Cost Spanning Tree
- Kruskals Algorithm
3 Graphs
- Used to model relationships between objects.
- Graphs consist of nodes and edges.
- The edges may or may not have direction.
- If they dont have a direction then they are
called as undirected graphs. - If they have a direction then they are called as
directed graphs.
4Examples of modeling with Graphs
(Directed Graph)
Graph of a road network (Undirected Graph)
5 Eulers Circuit problem
Problem Whether it was possible to start walking
from anywhere in town and return to the starting
point by crossing all bridges exactly once. This
is an undirected graph so one can walk in any
direction. In this case edges cant repeat but
vertices can. Degree of node Number of Edges
connected to it. Example Degree of C 5. In the
Eulers circuit problem, if you enter a node, you
have to exit it too, in order to cover all paths
(edges). Hence all the nodes should have an even
degree. In the above case, the nodes do not have
even degree hence it is not possible to start
walking from anywhere in town and return to the
starting point by crossing all bridges exactly
once. For a graph to be Eulers circuit all
nodes degrees should be even.
6Graph Terminologies
- A Graph consists of a set 'V' of vertices (or
nodes) and a set 'E' of edges (or links). - A graph can be directed or undirected.
- Edges in a directed graph are ordered pairs. The
order between the two vertices is important. - Example (S,P) is an ordered pair because the
edge - starts at S and terminates at P.
- The edge is unidirectional
- Edges of an undirected graph form unordered
pairs. - A multigraph is a graph with possibly several
edges between the same pair of vertices. - Graphs that are not multigraphs are called simple
graphs.
7Graphs Terminologies
8Graphs Terminologies
- The degree d(v) of a vertex v is the number of
edges incident to v. - d (A) three, d (D) two
- In directed graphs, indegree is the number of
incoming edges at the vertex and outdegree is the
number of outgoing edges from the vertex. - The indegree of P is 2, its outdegree is 1.
- The indegree of Q is 1, its outdegree is 1.
9Paths and Cycles
10Paths and Cycles
It is similar to the traveling salesman problem.
11Subgraphs
12Spanning tree
A spanning tree of a graph G is a subgraph of G
that is a tree and contains all the vertices of G.
13Connectivity
- A graph is said to be connected if there is a
path from any vertex to any other vertex in the
graph. - A forest is a graph that does not contain a
cycle. - A tree is a connected forest.
- A spanning forest of an undirected graph G is a
subgraph of G that is a forest and contains all
the vertices of G. - If a graph G(V,E) is not connected, then it can
be partitioned in a unique way into a set of
connected subgraphs called connected components. - A connected component of G is a connected
subgraph - of G such that no other connected subgraph of G
- contains it.
14Forest
15Graphs Representations in undirected graph
- Matrix
- 1 indicates that a link is present between the
nodes. - In case of an undirected graph we get a
symmetrical matrix.
- Edges are counted twice.
- For a graph of n nodes, size of matrix is n2.
- Maximum number of edges n(n-1)/2, because n
nodes are connected to n-1 nodes and counting
each link once so divide by 2.
16Graphs Representations in directed graph
- List
- The 0s from the matrix representation are
eliminated. - It has varying row length.
17Depth First Search (DFS)
- The algorithm starts from a node, selects a
node connected to it, then selects a node
connected to this new node and so on, till no new
nodes remain. The it backtracks to the latest
node and discovers any new nodes connected to it. - The data structure suitable for this purpose
(LIFO) is a stack. - Once stack is empty the algorithm ends.
18DFS
- DFS is an aggressive algorithm
- It finds a spanning tree
- Produces automatic ordering.
- Ordering not unique
- DFS is written recursively as it uses stack
- Application-connected components
19Algorithm for DFS
20Implementing DFS
- To draw a depth first search spanning tree lets
start with A. Put A in the stack. - Next we can pick either of D,E,B. Order does not
matter. In this example we r picking B. Put B in
stack. - After B we can either pick C or E. In this case
we put C in the stack. D is selected next and put
in stack. - After D there is just one edge to A. As A is
already visited, we start back track. We pop out
D and look at C. - From C there is one node left which is E. Add E
to stack. - From E again there is no other node so we pop E
out and look at C again. As there is no other
node left which needs to be explored so we pop C
out and similarly pop B and A out. - Finally as the stack is empty algorithm comes to
an end and we have corresponding tree .
21Running time of DFS
- V vertex set n
- E edges set O(n2)
- Running time in case of Adjacency List
representation O(VE) - We need to add V because there can be isolated
nodes. - Hence this is a linear time algorithm.
- If graph is very dense we can use matrix
representation. - Running time in case of Adjacency Matrix
representation O(n2)
22Breadth First Search (BFS)
- Compared to DFS breadth first search is a simple
algorithm - Timidly tries one edge. Totally exhaust neighbors
of a vertex then goes to next neighbors. It
radiates in waves in balanced manner. - Implemented using queues
- Whatever is in queues tells u what to explore
next. Once the queue is empty algorithm comes to
an end
23Algorithm for BFS
- Procedure BFS_Tree G(V,E)
- Input G (V,E) Q is a queue - initially empty
- x ?Q remove the front item of queue and
- denote it by x
- initially mark all vertices new
- Lx refers to the adjacency list of x.
- T ? 0
- Output The BFS tree T
- 1. v ?old v? V
- 2. insert (Q,v)
- 3. while Q is nonempty do
- 4. x ? Q
- 5. for each vertex w in Lx and
marked new - 6. T ? T ? x,w
- 7. w ? old
- 8. insert (Q,w)
24BFS
25BFS
26Implementing BFS using Queues
- Start from A. Add it to queue
- When visited A is removed from the queue, Add
D,E,B in queue (order does not matter)
A
D
E
B
C
E
B
- Next when D is pulled out vertex C which is
adjacent to D is added
A
C
27Running time of BFS
- G(V,E) , Vn, Em
- Running time of BFSO (n m)
- There is no cost to insert or delete
- When a vertex is inserted or deleted it is always
at the back of the queue
28Application of BFS Shortest Path
- Using BFS Shortest path to all the vertices
- To find shortest path from A to vertices
B,C,D,E,F we first create a spanning tree and
from that we can find shortest path - Figure 1 shows the graph and figure 2 shows the
corresponding spanning tree. - From A we can see the shortest path to reach E is
through A to C to E
Figure 1
Figure 2
29DFS Versus BFS
Graph
DFS
BFS
- DFS trees usually have thin and tall structure.
In case of complete graph (dense) it has one
straight line all the way. BFS trees usually are
bushy like a star. - In both trees, number of vertices is same. Both
are following the edges and hence can be used to
find connected components. - DFS can be recursively written. It is difficult
to do so in case of BFS.
30- DFS and BFS break graph in 3 parts
- visited nodes that have been visited.
- Fringe nodes that it will consider to visit
next. - Remaining vertices
- For DFS, the fringe nodes are in the stack while
for BFS they are in queue. - DFS and BFS each create a different shape.
- BFS
- DFS
Visited
Fringed
Remaining
31MCST
- MCST is minimum cost spanning tree . The
minimum-cost spanning tree (MCST) is one whose
edge weights add up to the least among all the
spanning trees - Minimum cost Spanning tree is a fixed connected
subgraph, containing all the vertices such that
the sum of the costs of the edges in the subgraph
is minimum also it does not contain any cycles. - A given graph may have more than one spanning
tree
32MCST
Consider a network of computers connected through
bidirectional links. Each link is associated with
a positive cost the cost of sending a message on
each link. The network is represented as an
undirected graph with positive costs on each
edge. In bidirectional networks we can assume
that the cost of sending a message on link does
not depend on the direction. Suppose we want to
broadcast a message to all the computers from an
arbitrary computer. The cost of the broadcast is
the sum of the costs of links used to forward the
message.
A
3
2
3
1
B
2
3
1
C
D
5
5
4
7
E
F
An undirected graph with positive costs on each
edge.
Graph with corresponding sorted order written
beside the edges
Minimum cost spanning tree
33MCST
- Why does the least cost edge have to be there in
MCST? - Proof using contradiction
- Assume minimum cost edge is not in the MCST.
- Assume it is between u and v(the dotted line
shown in the graph). - Since this edge is not in the MCST, there exist
another path between u and v. - Remove one edge (between x and y) and use the
minimum cost edge. - Now you get a cheaper tree.
- Hence least cost edge has to be there in MCST.
x
y
U
v
34Kruskals Algorithm
Graph is given, find MCST
35Kruskals Algorithm
A
3
3
3
2
1
2
B
1
2
1
6
4
C
D
6
4
3
4
5
4
7
5
7
5
E
2
8
F
8
- Figure on the left shows a graph with sorted
order (inside text boxes) ,cost written beside
the edges. Initially graph has only
vertices.Starting from A we can see A to C has
order one so this edge will be added first to
spanning tree
- Edge AD and AB are next as their order is 2 and
3 respectively
- Edge between C and D has order 4 but it creates
a cycle between ACD therefore it is not added to
the tree
- Next edge between D and F is added and edge
between B and C is dropped as it creates cycle
between A,B,C. Lastly edge between C,E is added
and edge between E,F is dropped
- Edges that do not belong to the tree are the
heavier ones than those that are in the tree.
36Kruskals Algorithm
- Running time
- Vn , Em
- Running time of sorting O (m log n)
37Example Why two already visited vertices are not
sufficient to check for a cycle
A
A
4
3
4
3
3
3
1
B
2
1
2
B
1
1
4
7
C
D
C
D
3
5
6
8
5
6
4
4
E
E
F
2
1
F
2
1
- Figure on the left shows a graph with sorted
order (inside text boxes) written beside the
edges plus the cost corresponding to each edge.
Starting from A we can see A to C has order one
so this edge will be added first to spanning tree
- Second in order is an edge between E and F so we
add it.
- Next we add edge between A and D and one edge
between A and B. Edge between C and D is
discarded as it creates a cycle
- Next an edge between D and F is added even if D
and F have been explored. Edge between B and C is
dropped and edge between C and E is dropped
38Example When an edge is added when its not.