0% found this document useful (0 votes)
3 views

Graph

Uploaded by

Rutuja Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Graph

Uploaded by

Rutuja Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Graph

defined as group of vertices and edges that are used to


connect these vertices.
defined as an ordered set G(V, E) where V(G) represents the
set of vertices and E(G) represents the set of edges which
are used to connect these vertices
Directed and Undirected Graph
Types of Graph
Directed
undirected graph
Undirected: edges are not associated with the directions
with them.
Directed and Undirected Graph
In a directed graph, edges form an ordered pair
Edges represent a specific path from some vertex A to
another vertex B.
Node A is called initial node while node B is called terminal
node
Weighted Graph
• each edge is assigned with some data such as length or weight.
• The weight of an edge e can be given as w(e) which must be a positive (+)
value indicating th

• e cost of traversing the edge .


Graph Terminology
Path:
defined as the sequence of nodes that are followed in order to
reach some terminal node V from the initial node U.
Closed Path:
A path will be called as closed path if the initial node is
same as terminal node. A path will be closed path if V0=VN.
Graph Terminology
Cycle
A cycle can be defined as the path which has no repeated edges or vertices
except the first and last vertices.
Complete Graph
Undirected graph of n vertices
every node is connected with all other nodes.
A complete graph contain n(n-1)/2 edges where n is the number of nodes
in the graph
Fig1:2 vertices so edges=2(2-1)/2=1
Fig 3: 4 vertices so edges=4(4-1)/2=6
Connected Graph
• A connected graph is the one in which some path exists between every
two vertices (u, v) in V. There are no isolated nodes in connected graph.
Representation of Graph in computer
✔ Adjacency Matrix

✔ Adjacency List
Adjacency matrix
✔ A way of representing a graph as a matrix of
booleans (0's and 1's).
✔ represented in the form of a square matrix on
a computer
✔ boolean value of the matrix indicates if there
is a direct path between two vertices.
Adjacency matrix
Adjacency List

✔ represents a graph as an array of linked lists.

✔ The index of the array represents a vertex

✔ each element in its linked list represents the


other vertices that form an edge with the
vertex.
Adjacency List
Transitive closure

✔ Given a directed graph, find out if a vertex j is


reachable from another vertex i for all vertex
pairs (i, j) in the given graph.

✔ Here reachable mean that there is a path


from vertex i to j.

✔ The reach-ability matrix is called the


transitive closure of a graph.
Transitive closure
R={(a,b),(d,a)}

RT={(d,b)}
Transitive closure
R={(a,b),(d,b),(b,d)}
RT={(a,d),(d,d),((b,b)}
Transitive closure
R={(a,d),(b,d),(d,d),(d,a),(d,b),(d,c)}
RT={(a,a),(a,b),(a,c),(b,a),(b,d),(b,c),(d,b),(d,c),
(d,d)}
DEPTH FIRST SEARCH(DFS)

• The purpose of the algorithm is to mark each vertex as visited


while avoiding cycles.

• The DFS algorithm works as follows:


• Start by putting any one of the graph's vertices on top of a stack.

• Take the top item of the stack and add it to the visited list.

• Create a list of that vertex's adjacent nodes. Add the ones which
aren't in the visited list to the top of the stack.

• Keep repeating steps 2 and 3 until the stack is empty.


DEPTH FIRST SEARCH
Adjacency Matrix
0 1 2 3 4
0 0 1 1 1 0
1 1 0 0 0 0
2 1 0 0 0 1
3 1 0 0 0 0
4 0 0 1 0 0

Visited 0 1 2 4 3
DEPTH FIRST SEARCH(DFS)

Adjacency Matrix
A B C D E F
A 0 1 1 1 0 0
B 1 0 0 0 1 1
C 1 0 0 0 0 1 visited A B E F C D
D 1 0 0 0 0 0
E 0 1 0 0 0 0
F 0 1 1 0 0 0
Breadth first search(BFS)

• The purpose of the algorithm is to mark each vertex as


visited while avoiding cycles.
• The algorithm works as follows:
• Start by putting any one of the graph's vertices at the
back of a queue.
• Take the front item of the queue and add it to the
visited list.
• Create a list of that vertex's adjacent nodes. Add the
ones which aren't in the visited list to the back of the
queue.
• Keep repeating steps 2 and 3 until the queue is empty.
Breadth first search(BFS)
Breadth first search(BFS)
Breadth first search(BFS)
Breadth first search(BFS)
Breadth first search(BFS)
Breadth first search(BFS)
SPANNING TREE
A spanning tree is a sub-graph of an
undirected connected graph, which includes
all the vertices of the graph with a minimum
possible number of edges.
If a vertex is missed, then it is not a spanning
tree.
The edges may or may not have weights
assigned to them
SPANNING TREE

.
SPANNING TREE
Minimum Spanning Tree

A minimum spanning tree is a spanning tree in which the sum of


the weight of the edges is as minimum as possible
Applications
Minimum Spanning Tree
Algorithms to find minimum spanning tree

Prims Algorithm
Kruskal Algorithm
PRIMS ALGORITHM
Prim’s Algorithm is a famous greedy
algorithm.
It is used for finding the Minimum Spanning
Tree (MST) of a given graph.
To apply Prim’s algorithm, the given graph
must be weighted, connected and undirected.
PRIMS ALGORITHM
• Step-01:

• Randomly choose any vertex.
• The vertex connecting to the edge having least weight is usually
selected.

• Step-02:

• Find all the edges that connect the tree to new vertices.
• Find the least weight edge among those edges and include it in
the existing tree.
• If including that edge creates a cycle, then reject that edge and
look for the next least weight edge
PRIMS ALGORITHM
• Step-03:

• Keep repeating step-02 until all the vertices are
included and Minimum Spanning Tree (MST) is
obtained.

• Prim’s Algorithm Time Complexity-

• Worst case time complexity of Prim’s Algorithm is-
• O(ElogV) using binary heap
• O(E + VlogV) using Fibonacci heap
PRIMS ALGORITHM

1 2 28
✔ 1 6 10
PRIMS ALGORITHM
1 2 28
✔ 1 6 25
✔ 6 5 25
PRIMS ALGORITHM
1 2 28
✔ 1 6 25
✔ 6 5 25
✔ 54 22
57 24
PRIMS ALGORITHM
1 2 28
✔ 1 6 25
✔ 6 5 25
✔ 54 22
57 24
4 7 18
✔ 4 3 12
PRIMS ALGORITHM
12 28
✔ 16 25
✔ 65 25
✔ 54 22
57 24
47 18
✔ 43 12
✔ 32 16
PRIMS ALGORITHM
12 28
✔ 16 25
✔ 65 25
✔ 54 22
57 24
47 18
✔ 43 12
✔ 32 16 Since all the vertices have been included in the
MST, so we stop.
✔ 27 14
Now, Cost of Minimum Spanning Tree
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
PRIMS ALGORITHM
PRIMS ALGORITHM

Now, Cost of Minimum Spanning Tree


= Sum of all edge weights
= 1 + 4 + 2 + 6 + 3 + 10
= 26 units
KRUSKAL ALGORITHM
Kruskal’s Algorithm is a famous greedy
algorithm.
It is used for finding the Minimum Spanning
Tree (MST) of a given graph.
To apply Kruskal’s algorithm, the given graph
must be weighted, connected and undirected.
KRUSKAL ALGORITHM
• Step-01:

• Sort all the edges from low weight to high weight.

• Step-02:

• Take the edge with the lowest weight and use it to connect the vertices of
graph.
• If adding an edge creates a cycle, then reject that edge and go for the next
least weight edge.

• Step-03:

• Keep adding edges until all the vertices are connected and a Minimum
Spanning Tree (MST) is obtained.
KRUSKAL ALGORITHM
KRUSKAL ALGORITHM
KRUSKAL ALGORITHM
KRUSKAL ALGORITHM
KRUSKAL ALGORITHM
KRUSKAL ALGORITHM
Since all the vertices have been connected /
included in the MST, so we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy