0% found this document useful (0 votes)
18 views20 pages

Graph Theory

The document discusses two types of graphs: directed and undirected, and explains graph algorithms, specifically Prim's and Kruskal's algorithms for finding minimum spanning trees. Prim's algorithm builds a minimum spanning tree by adding the smallest connecting edge from a starting node, while Kruskal's algorithm selects edges based on weight, avoiding cycles. The document also includes examples and activities related to applying these algorithms in practical scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views20 pages

Graph Theory

The document discusses two types of graphs: directed and undirected, and explains graph algorithms, specifically Prim's and Kruskal's algorithms for finding minimum spanning trees. Prim's algorithm builds a minimum spanning tree by adding the smallest connecting edge from a starting node, while Kruskal's algorithm selects edges based on weight, avoiding cycles. The document also includes examples and activities related to applying these algorithms in practical scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Graph Theory:

Graph Algorithm;
Prim; Kruskal
TWO TYPES OF GRAPH

DIRECTEDGRAPH
UNDIRECTED GRAPH
What is a
Directed
Graph?
 A graph is a Directed Graph
when the pair of vertices
representing any edge by an
ordered or a direct pair (V1,
V2)
 Where V1, is the tail and V2 is
the head of the edge.
 Thus, <V1, V2> and <V2,
V1> represent two different
edges.

V1 V2
What is an
Undirected Graph?

 A graph is called
Undirected Graph
when the pair of
vertices
representing any
edge is unordered.
 Thus <V1,V2> and
<V2,V1> represent
the same.
What is Graph
Algorithm?
 A graph algorithm is a step-by-step
method used to solve problems related
to graphs in computer science and
mathematics. A graph is made up of
nodes (vertices) and edges
(connections between nodes). These
algorithms help us find things like the
shortest path between two points,
whether two nodes are connected, or the
best way to visit all nodes.
STEP-BY-STEP of Graph Algorithm
2️⃣Choose the Right
11️⃣ Represent the
Algorithm → Pick BFS (for
Graph → Use an adjacency
level-order search), DFS (for
list or adjacency matrix to
deep search), Dijkstra (for
store connections between
shortest path), or Kruskal/Prim
nodes.
(for minimum spanning tree).

4️⃣Return the Result → Stop


3️⃣Process Nodes & Neighbors
when all nodes are visited or
→ Start from a node, visit its
the goal is reached (like finding
neighbors, and update distances
the shortest path or spanning
or paths if needed.
tree).
EXAMPLE OF
GRAPH
ALGORITHM
PRIM’S ALGORITHM

The algorithm was Prim’s algorithm finds the


then rediscovered by Minimum Spanning Tree
Prim's algorithm (MST) by starting with a
was invented in Robert C. Prim in
random node and
1957, and also
1930 by the rediscovered by
repeatedly adding the
smallest connecting edge
Czech Edsger W. Dijkstra until all nodes are included.
mathematician in 1959. Therefore, It follows a greedy
Vojtěch Jarník. the algorithm is also approach and works only
sometimes called on connected graphs; for
"Jarník's algorithm", unconnected graphs,
Kruskal’s algorithm is
or the "Prim-Jarník used instead.
algorithm".
PURPOSE OF PRIMS
ALGORITHM
The purpose of Prim’s
Algorithm is to find a subset
of the edges that forms a tree
which includes every vertex
where the total weight of all of
the edges is a minimum.
PRIM – MINIMUM SPANNING TREE

 Choose an arbitrary start vertex


 Keep including connected edges

SUBGRAPH
 All vertices are connected
 No CYCLES
EXAMPLE
 Pauline is a gardener and has
created a sprinkler system in
the given diagram. Using
Prim’s algorithm, determine
the network that will connect
all of the sprinklers with the
least amount of piping and
determine the total length of
piping needed. Each vertex
represents a sprinkler and the
weight of each edge represents
the distance in meters.
ACTIVITY 1
 The management of a Kerry campsite
wants to connect each mobile home with
running water in the easiest way possible.
Each mobile home is represented by a
letter and the weight on each edge
represents the distance between the
mobile homes in metres.
 (i) Determine the Minimum Spanning
Tree so that every mobile home is
connected to running water using the
least length of piping.
 (ii) Calculate the total length of pipe
needed.
ACTIVITY 2

 An electrician wants to wire


sockets for a house using the
least amount of wiring
ensuring that they are all
connected to each other. Draw
the minimum spanning tree of
the following graph and
determine the total length of
wiring needed. All distances
are in metres.
ACTIVITY 3,4
 Create a minimum spanning tree.

3 4
ACTIVITY 5
 Create a minimum spanning tree.
KRUSKAL’S
ALGORITHM
What is Kruskal Algorithm?

KRUSKAL algorithm is a greedy


algorithm which is where it builds up
a solution piece by piece, always
choosing the next piece that offers
the most obvious and immediate
benefit.
STEP-BY-STEP of Kruskal Algorithm

1. To begin, select the edge of least weight.


2. Find the next edge of least weight. If it would form a cycle
with the edges already selected, don’t choose it. If not then
add it to the MST.
3. If there is a choice of equal edges, it has no effect which
you choose first.
4. Repeat step 2 until all vertices are connected.
EXAMPLE:
 In New York, there is a park with famous statues that
has many visitors each
 day. Lighting is to be installed at 5 places in the park
(as shown) with the
 places being connected either directly or indirectly
by cabling following the
 given paths.
 The values on each edge represent the distance (in m)
between each light.
 (a) Calculate the shortest length of cabling required
and show the
 minimum spanning tree (MST).
 (b) State two differences between Kruskal’s
algorithm and Prim’s
 algorithm for finding a minimum spanning tree.

You might also like