SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Prim's Algorithm
    on minimum spanning tree



Submitted by:
Abdullah Al Mamun (Oronno)
Department of Computer Science & Engineering,
University of Dhaka.
2nd Year, Session: 2006-07
What is Minimum Spanning Tree?
• Given a connected, undirected graph, a
  spanning tree of that graph is a subgraph
  which is a tree and connects all the
  vertices together.
• A single graph can have many different
  spanning trees.
• A minimum spanning tree is then
  a spanning tree with weight less
  than or equal to the weight of
  every other spanning tree.
graph G




Spanning Tree from Graph G
2            2

    4    3             4     5


1            1          1
Algorithm for finding
  Minimum Spanning Tree


• The Prim's Algorithm
• Kruskal's Algorithm
• Baruvka's Algorithm
About Prim’s Algorithm
The algorithm was discovered in 1930 by
mathematician Vojtech Jarnik and later independently
by computer scientist Robert C. Prim in 1957.
The algorithm continuously increases the size of a
tree starting with a single vertex until it spans all the
vertices.
 Prim's algorithm is faster on dense
graphs.
Prim's algorithm runs in O(n*n)
But the running time can be reduce
using a simple binary heap data structure
and an adjacency list representation
• Prim's algorithm for finding a minimal
  spanning tree parallels closely the depth-
  and breadth-first traversal algorithms. Just
  as these algorithms maintained a closed
  list of nodes and the paths leading to
  them, Prim's algorithm maintains a closed
  list of nodes and the edges that link them
  into the minimal spanning tree.
• Whereas the depth-first algorithm used a
  stack as its data structure to maintain the
  list of open nodes and the breadth-first
  traversal used a queue, Prim's uses a
  priority queue.
Let’s see an example to
       understand
   Prim’s Algorithm.
Lets….
 At first we declare an array named: closed list.

 And consider the open list as a priority queue
  with min-heap.
 Adding a node and its edge to the closed list
  indicates that we have found an edge that links
  the node into the minimal spanning
  tree. As a node is added to the
  closed list, its successors
  (immediately adjacent nodes)
  are examined and added to a
  priority queue of open nodes.
Total Cost: 0




Open List: d
Close List:
Total Cost: 0




Open List: a, f, e, b
Close List: d
Total Cost: 5




Open List: f, e, b
Close List: d, a
Total Cost: 11




Open List: b, e, g
Close List: d, a, f
Total Cost: 18




Open List: e, g, c
Close List: d, a, f, b
Total Cost: 25




Open List: c, g
Close List: d, a, f, b, e
Total Cost: 30




Open List: g
Close List: d, a, f, b, e, c
Total Cost: 39




Open List:
Close List: d, a, f, b, e, c
PSEUDO-CODE FOR PRIM'S
     ALGORITHM
   Designate one node as the start node
   Add the start node to the priority queue of open nodes.
   WHILE (there are still nodes to be added to the closed list)
    {
      Remove a node from priority queue of open nodes, designate it as current
      node.
      IF (the current node is not already in the closed list)
         {
         IF the node is not the first node removed from the priority queue, add the
        minimal edge connecting it with a closed node to the minimal spanning
        tree.
         Add the current node to the closed list.
         FOR each successor of current node
              IF (the successor is not already in the closed list OR the successor is
             now connected to a closed node by an edge of lesser weight than
             before)
                    Add that successor to the priority queue of open nodes;
         }
    }
Sample C++ Implementation
•   void prim(graph &g, vert s) {                              •   int minvertex(graph &g, int *d) {
                                                                •     int v;
•       int dist[g.num_nodes];
•       int vert[g.num_nodes];                                  •       for (i = 0; i < g.num_nodes; i++)
                                                                •         if (g.is_marked(i, UNVISITED)) {
•       for (int i = 0; i < g.num_nodes; i++) {                 •           v = i; break;
•         dist[i] = INFINITY;                                   •         }

•       dist[s.number()] = 0;                                   •    for (i = 0; i < g.num_nodes; i++)
                                                                •      if ((g.is_marked(i, UNVISITED)) && (dist[i] <
                                                                    dist[v])) v = i;
•       for (i = 0; i < g.num_nodes; i++) {
•         vert v = minvertex(g, dist);
                                                                •       return (v);
                                                                •   }
•           g.mark(v, VISITED);
•           if (v != s) add_edge_to_MST(vert[v], v);
•           if (dist[v] == INFINITY) return;

•           for (edge w = g.first_edge; g.is_edge(w), w = g.next_edge(w)) {
•             if (dist[g.first_vert(w)] = g.weight(w)) {
•                dist[g.second_vert(w)] = g.weight(w);
•                   vert[g.second_vert(w)] = v;
•             }
•           }
•       }
•   }
Complexity Analysis
 Minimum edge weight data     Time complexity (total)
        structure

adjacency matrix, searching   O(V*V)


binary heap and adjacency     O((V + E) log(V)) = O(E
list                          log(V))

Fibonacci heap and            O(E + V log(V))
adjacency list
Application
 One practical application of a MST would be in the design of a
  network. For instance, a group of individuals, who are
  separated by varying distances, wish to be connected together
  in a telephone network. Because the cost between two terminal
  is different, if we want to reduce our expenses, Prim's
  Algorithm is a way to solve it
 Connect all computers in a computer science building using
  least amount of cable.
 A less obvious application is that the minimum spanning tree
  can be used to approximately solve the traveling salesman
  problem. A convenient formal way of defining this problem is
  to find the shortest path that visits each point at least once.
 Another useful application of MST would be finding airline
  routes. The vertices of the graph would represent cities, and
  the edges would represent routes between the cities.
  Obviously, the further one has to travel, the more it will cost,
  so MST can be applied to optimize airline routes by finding
  the least costly paths with no cycles.
Reference
Book:
  Introduction to Algorithm
  By: Thomas H. Cormen
Website:
• mathworld.wolfram.com
• www-b2.is.tokushima-u.ac.jp/suuri/Prim.shtml
• www.cprogramming.com/tutorial/
  computersciencetheory/mst.html
• en.wikipedia.org/wiki/Prim's_algorithm
• www.unf.edu/~wkloster/foundations/
  PrimApplet/PrimApplet.htm
Thank you for being with me…


     Any question?????

Weitere ähnliche Inhalte

Was ist angesagt?

Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithmsSaga Valsalan
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmGaurav Kolekar
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)Madhu Bala
 
Topological Sorting
Topological SortingTopological Sorting
Topological SortingShahDhruv21
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims AlgorithmSriram Raj
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraRoshan Tailor
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentationSubid Biswas
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's AlgorithmArijitDhali
 

Was ist angesagt? (20)

Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims Algorithm
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 

Ähnlich wie Prim's Algorithm on minimum spanning tree

Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsSigSegVSquad
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaPyData
 
Social network-analysis-in-python
Social network-analysis-in-pythonSocial network-analysis-in-python
Social network-analysis-in-pythonJoe OntheRocks
 
Link Prediction in the Real World
Link Prediction in the Real WorldLink Prediction in the Real World
Link Prediction in the Real WorldBalaji Ganesan
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2MuradAmn
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
lecture 17
lecture 17lecture 17
lecture 17sajinsc
 
hospital management
hospital managementhospital management
hospital managementguestbcbbb5c
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxwhittemorelucilla
 
FADML 06 PPC Graphs and Traversals.pdf
FADML 06 PPC Graphs and Traversals.pdfFADML 06 PPC Graphs and Traversals.pdf
FADML 06 PPC Graphs and Traversals.pdfYelah1
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfiftakhar8
 

Ähnlich wie Prim's Algorithm on minimum spanning tree (20)

Ppt 1
Ppt 1Ppt 1
Ppt 1
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Unit ii-ppt
Unit ii-pptUnit ii-ppt
Unit ii-ppt
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Social network-analysis-in-python
Social network-analysis-in-pythonSocial network-analysis-in-python
Social network-analysis-in-python
 
Link Prediction in the Real World
Link Prediction in the Real WorldLink Prediction in the Real World
Link Prediction in the Real World
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
lecture 17
lecture 17lecture 17
lecture 17
 
8150.graphs
8150.graphs8150.graphs
8150.graphs
 
hospital management
hospital managementhospital management
hospital management
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
d
dd
d
 
FADML 06 PPC Graphs and Traversals.pdf
FADML 06 PPC Graphs and Traversals.pdfFADML 06 PPC Graphs and Traversals.pdf
FADML 06 PPC Graphs and Traversals.pdf
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
 

Kürzlich hochgeladen

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...liera silvan
 

Kürzlich hochgeladen (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
 

Prim's Algorithm on minimum spanning tree

  • 1. Prim's Algorithm on minimum spanning tree Submitted by: Abdullah Al Mamun (Oronno) Department of Computer Science & Engineering, University of Dhaka. 2nd Year, Session: 2006-07
  • 2. What is Minimum Spanning Tree? • Given a connected, undirected graph, a spanning tree of that graph is a subgraph which is a tree and connects all the vertices together. • A single graph can have many different spanning trees. • A minimum spanning tree is then a spanning tree with weight less than or equal to the weight of every other spanning tree.
  • 3. graph G Spanning Tree from Graph G 2 2 4 3 4 5 1 1 1
  • 4. Algorithm for finding Minimum Spanning Tree • The Prim's Algorithm • Kruskal's Algorithm • Baruvka's Algorithm
  • 5. About Prim’s Algorithm The algorithm was discovered in 1930 by mathematician Vojtech Jarnik and later independently by computer scientist Robert C. Prim in 1957. The algorithm continuously increases the size of a tree starting with a single vertex until it spans all the vertices. Prim's algorithm is faster on dense graphs. Prim's algorithm runs in O(n*n) But the running time can be reduce using a simple binary heap data structure and an adjacency list representation
  • 6. • Prim's algorithm for finding a minimal spanning tree parallels closely the depth- and breadth-first traversal algorithms. Just as these algorithms maintained a closed list of nodes and the paths leading to them, Prim's algorithm maintains a closed list of nodes and the edges that link them into the minimal spanning tree. • Whereas the depth-first algorithm used a stack as its data structure to maintain the list of open nodes and the breadth-first traversal used a queue, Prim's uses a priority queue.
  • 7. Let’s see an example to understand Prim’s Algorithm.
  • 8. Lets….  At first we declare an array named: closed list.  And consider the open list as a priority queue with min-heap.  Adding a node and its edge to the closed list indicates that we have found an edge that links the node into the minimal spanning tree. As a node is added to the closed list, its successors (immediately adjacent nodes) are examined and added to a priority queue of open nodes.
  • 9. Total Cost: 0 Open List: d Close List:
  • 10. Total Cost: 0 Open List: a, f, e, b Close List: d
  • 11. Total Cost: 5 Open List: f, e, b Close List: d, a
  • 12. Total Cost: 11 Open List: b, e, g Close List: d, a, f
  • 13. Total Cost: 18 Open List: e, g, c Close List: d, a, f, b
  • 14. Total Cost: 25 Open List: c, g Close List: d, a, f, b, e
  • 15. Total Cost: 30 Open List: g Close List: d, a, f, b, e, c
  • 16. Total Cost: 39 Open List: Close List: d, a, f, b, e, c
  • 17. PSEUDO-CODE FOR PRIM'S ALGORITHM  Designate one node as the start node  Add the start node to the priority queue of open nodes.  WHILE (there are still nodes to be added to the closed list) { Remove a node from priority queue of open nodes, designate it as current node. IF (the current node is not already in the closed list) { IF the node is not the first node removed from the priority queue, add the minimal edge connecting it with a closed node to the minimal spanning tree. Add the current node to the closed list. FOR each successor of current node IF (the successor is not already in the closed list OR the successor is now connected to a closed node by an edge of lesser weight than before) Add that successor to the priority queue of open nodes; } }
  • 18. Sample C++ Implementation • void prim(graph &g, vert s) { • int minvertex(graph &g, int *d) { • int v; • int dist[g.num_nodes]; • int vert[g.num_nodes]; • for (i = 0; i < g.num_nodes; i++) • if (g.is_marked(i, UNVISITED)) { • for (int i = 0; i < g.num_nodes; i++) { • v = i; break; • dist[i] = INFINITY; • } • dist[s.number()] = 0; • for (i = 0; i < g.num_nodes; i++) • if ((g.is_marked(i, UNVISITED)) && (dist[i] < dist[v])) v = i; • for (i = 0; i < g.num_nodes; i++) { • vert v = minvertex(g, dist); • return (v); • } • g.mark(v, VISITED); • if (v != s) add_edge_to_MST(vert[v], v); • if (dist[v] == INFINITY) return; • for (edge w = g.first_edge; g.is_edge(w), w = g.next_edge(w)) { • if (dist[g.first_vert(w)] = g.weight(w)) { • dist[g.second_vert(w)] = g.weight(w); • vert[g.second_vert(w)] = v; • } • } • } • }
  • 19. Complexity Analysis Minimum edge weight data Time complexity (total) structure adjacency matrix, searching O(V*V) binary heap and adjacency O((V + E) log(V)) = O(E list log(V)) Fibonacci heap and O(E + V log(V)) adjacency list
  • 20. Application  One practical application of a MST would be in the design of a network. For instance, a group of individuals, who are separated by varying distances, wish to be connected together in a telephone network. Because the cost between two terminal is different, if we want to reduce our expenses, Prim's Algorithm is a way to solve it  Connect all computers in a computer science building using least amount of cable.  A less obvious application is that the minimum spanning tree can be used to approximately solve the traveling salesman problem. A convenient formal way of defining this problem is to find the shortest path that visits each point at least once.  Another useful application of MST would be finding airline routes. The vertices of the graph would represent cities, and the edges would represent routes between the cities. Obviously, the further one has to travel, the more it will cost, so MST can be applied to optimize airline routes by finding the least costly paths with no cycles.
  • 21. Reference Book: Introduction to Algorithm By: Thomas H. Cormen Website: • mathworld.wolfram.com • www-b2.is.tokushima-u.ac.jp/suuri/Prim.shtml • www.cprogramming.com/tutorial/ computersciencetheory/mst.html • en.wikipedia.org/wiki/Prim's_algorithm • www.unf.edu/~wkloster/foundations/ PrimApplet/PrimApplet.htm
  • 22. Thank you for being with me… Any question?????