Code Examples
  Home arrow Code Examples arrow Page 3 - The Prufer Code and the Floyd-Warshall Alg...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

The Prufer Code and the Floyd-Warshall Algorithm
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-05-07

    Table of Contents:
  • The Prufer Code and the Floyd-Warshall Algorithm
  • How Prufer Works
  • Decoding Prufer
  • Floyd-Warshall Algorithm

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Prufer Code and the Floyd-Warshall Algorithm - Decoding Prufer


    (Page 3 of 4 )

    If we code something and fail to decode it, that probably would be meaningless, so here we will go in the other direction. First, we have to add the n to the last position. As I said before, the last item is there by default, so it is not added to the sequence (even if we do not know the number of vertexes, this will be the length of the sequence plus two).

    The algorithm will try, and it will succeed, to determine at every step another edge of the tree. Therefore, this also will have n-1 steps. When we add the n to the sequence, the length of the sequence will turn into n-1.

    The algorithm is simple. When we wrote down at code time the "i"th item that was not cut before, it will not be cut in the future either; it was the smallest indexed vertex at that time that still was related to the tree.

    In conclusion, we need to find the smallest index in the sequence containing the points that were cut down, and the one present in the Prüfer code. Look at the image below in order to comprehend it better.

    The upper row is the Prüfer code, and for the next item we will always choose the smallest not-existing index from the grayed numbers (those that will be added later and those that were already added to the code). In the end, the two- dimensional array will hold one edge in each column. Here is everything coded in C:

    for ( j =0; j < n-1; ++j)

    {

    memset(present,0, n*sizeof(int) ) ; // reset the count // array - here present[i] will be zero if it's grayed

     

    for( k = j ; k < n-2; ++k)

    present[prufer[k]] = 1; // the items in the code

     

    for( k = 0; k < j; k++) // points already added

    present[temp[k]] = 1;

     

    for( k =0 ; k < n; ++k)

    if(!present[k]) // first not present

    {

    // add to our edge list

    edgeList[j].from = prufer[j] ;

    edgeList[j].to = k;

    temp[j] = k; // add it as already deduced vertex

    break; // end this loop...

    }

    }

    With one input of:

    8

    1 2

    1 6

    1 7

    0 1

    0 3

    0 4

    4 5

    The result:

    Edges:

    1 - 2

    1 - 6

    1 - 7

    0 - 1

    0 - 3

    0 - 4

    4 - 5

     

    $$$$$$$$$$ 0 1 2 3 4 5 6 7

    A Degrees: 3 4 1 1 2 1 1 1

    The prufer code:

    1 0 4 0 1 1 7

     

    The prufer code transformed back: edges

    1 - 2

    0 - 3

    4 - 5

    0 - 4

    1 - 0

    1 - 6

    7 - 1

     

    Press any key to continue . . .

    Here is the code if you want to have a look at it; it's a simple C file, so just compile it. Assure that you have a valid input within the In.txt file and it should work perfectly:

    Download Source

     

    More Code Examples Articles
    More By Gabor Bernat


     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek