Click here to Skip to main content
15,890,336 members
Articles / Programming Languages / C#
Article

Finding Shortest Path Recursively

Rate me:
Please Sign up or sign in to vote.
3.83/5 (4 votes)
10 Sep 2014CPOL1 min read 44.1K   1.1K   9   13
Finding shortest path in a path collection from starting point to target point

Image 1

Introduction

There are several ways to find the shortest path in a given path collection from a starting point to target point. In this tip, I will try to show how to find the shortest path recursively. The main idea is, a walker walks through all possible paths to reach the target point and decides the shortest path. This manner is the most cost-efficient way, but this is just another solution to find the shortest path.

Using the Code

There are 2 classes. One of them is the Path class that simulates a path, having two points and a distance value. Path is considered as one directional.

C#
/// <summary>
/// Definition of a path
/// </summary>
public class Path
{
    //beginning and ending points
    string startingPoint, endingPoint;

    //distance of path
    double distance;
    ...

The other class is the Walker class. This class is the main actor of the program. Holds all paths, walks from a starting point to target point to find all possibilities and decides the shortest path.

C#
// <summary>
/// Main actor of application.
/// Holds full paths
/// Walks through all combination of path to reach target point.
/// starts walking from a starting point
/// </summary>
public class Walker
{
    string startingPoint = string.Empty, targetPoint = string.Empty;

    //Holds all paths in memory
    List<Path> allPaths;

    //contains the shortest path
    List<Path> shortestPath;

    //contains currently experiencing path
    List<Path> currentPath;
    ...

The most important method of walker class is Experience(string point). Walker walks through all possible paths and decides the shortest path.

C#
/// <summary>
/// Experience all combination of paths from a given point
/// </summary>
/// <param name="point"></param>
public void Experience(string point)
{
    Console.WriteLine("Experiencing all paths from point {0}", point);
    List<Path> availablePaths = GetAvailablePaths(point);
    foreach (Path path in availablePaths)
    {
        if (Move(path))
        {
            //if target point is reached then check whether current path is shorter than shortestPath
            if (path.EndingPoint == this.targetPoint)
            {
                //Decide if current path is shorter than shortest path
                if(TotalDistance(this.shortestPath) == 0 || 
                TotalDistance(this.shortestPath) > TotalDistance(this.currentPath))
                    SetShortestPath(this.currentPath);
            }
            else
                //try to reach targetpoint by walking recursively available paths
                Experience(path.EndingPoint);
        }
        //After experiencing all paths from point go back to get the original state of current path
        this.currentPath.Remove(path);
    }
    Console.WriteLine("Experienced all paths from point {0}", point);
}

Points of Interest

Although the title is written as 'Finding shortest path', it is possible to find other paths, such as longest path, path that has minimum or maximum steps, etc. Just change decision making in Experience(string point) method. Path is considered as one directional. If a point A to B is defined, then defining a path that is B to C can simulate two directional.

History

  • September 11, 2014: Added ShortestPathRecursively

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Turkey Turkey
Working on C#,C++,PL/SQL,Oracle,Microsoft Technologies since 2005

Comments and Discussions

 
Questionwhy you dont use Dijkstra? Pin
Member 1107038011-Sep-14 8:31
Member 1107038011-Sep-14 8:31 
QuestionPath class is unidirectional Pin
Michael S. Meyers-Jouan11-Sep-14 7:26
Michael S. Meyers-Jouan11-Sep-14 7:26 
GeneralRe: Path class is unidirectional Pin
PIEBALDconsult11-Sep-14 7:55
mvePIEBALDconsult11-Sep-14 7:55 
GeneralRe: Path class is unidirectional Pin
Michael S. Meyers-Jouan11-Sep-14 8:08
Michael S. Meyers-Jouan11-Sep-14 8:08 
GeneralRe: Path class is unidirectional Pin
PIEBALDconsult11-Sep-14 8:12
mvePIEBALDconsult11-Sep-14 8:12 
GeneralRe: Path class is unidirectional Pin
Michael S. Meyers-Jouan11-Sep-14 14:22
Michael S. Meyers-Jouan11-Sep-14 14:22 
GeneralRe: Path class is unidirectional Pin
PIEBALDconsult11-Sep-14 14:24
mvePIEBALDconsult11-Sep-14 14:24 
GeneralRe: Path class is unidirectional Pin
Michael S. Meyers-Jouan11-Sep-14 14:57
Michael S. Meyers-Jouan11-Sep-14 14:57 
QuestionNot a very good solution Pin
Erich Ledesma10-Sep-14 22:44
Erich Ledesma10-Sep-14 22:44 
AnswerRe: Not a very good solution Pin
Buraks11-Sep-14 2:55
Buraks11-Sep-14 2:55 
GeneralThoughts Pin
PIEBALDconsult10-Sep-14 8:39
mvePIEBALDconsult10-Sep-14 8:39 
QuestionWhat if... Pin
Wendelius10-Sep-14 8:26
mentorWendelius10-Sep-14 8:26 
AnswerRe: What if... Pin
Buraks10-Sep-14 19:54
Buraks10-Sep-14 19:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.