Click here to Skip to main content
Click here to Skip to main content

C#: A-Star is born

, 23 Jun 2003
Rate this:
Please Sign up or sign in to vote.
Understand graphs and A* path-finding algorithm with C#

Automatic path finding with GraphFormer

Introduction: what is it supposed to do ?

I dedicated myself to creating a .NET component that allows the building of graphs and the performance of some operations on these structures. In particular A*, the famous path finding algorithm, can be run to find the best path between two places.

First and foremost it should be kept in mind that a graph is defined with :

  • A list of nodes
  • A list of arcs

Each node is defined with the following data :

  • A geographical position in space (co-ordinates X,Y,Z)
  • The collection of the outgoing arcs
  • The collection of the incoming arcs

Each arc is simply defined with its two extremity nodes. So an arc is oriented from StartNode to EndNode. It is also characterized by a crossing factor named 'Weight'. This value represents the difficulty to reach the ending node from the starting one. Thus, getting through an arc has a cost, which can be basically calculated like this :

Cost = Distance(StartNode, EndNode) * Weight

You may be asking "What is the use of this structure ?...". Well, the applications can be various, ranging from road maps and circulation models to a character's mobility in a video game. Now the question is: "How one would move across this graph ?".

What is A* ?

Imagine that you are on a node and that you want to reach another position somewhere else on the graph. Then ask : "Which way will I follow, and why ?". The main factor to take into account here is the cost of moving. It must be minimal. The cost criterion is basically a function of the distance (sum of arcs' lengths). However, it can also be adjusted and varied with other data, which describe for example the slope, the harshness/practicability of the ground. You can even model a traffic jam.

To achieve the best path, there are many algorithms which are more or less effective, depending on the particular case. Efficiency depends not only on the time needed for calculation, but also on the reliability of the result. A* is able to return the best path (if it exists) between two nodes, according to accessibility/orientation and, of course, cost of arcs.

Among the variety of existing algorithms, some do not always actually return the best path, but they can give precedence to speed over accuracy. Efficiency depends on the number of nodes and on their geographical distribution. However in most cases A* turns out to be the most effective, because it combines optimized search with the use of a heuristic.

A heuristic is a function that associates a value with a node to gauge it. One node is considered better than another, if the final point is reached with less effort (e.g. shorter distance).

A* will always return the shortest path if, and only if, the heuristic is admissible; that is to say, if it never overestimates. On the other hand, if the heuristic is not admissible, A* will find a path in less time and with less memory usage, but without the absolute guaranty that it is the best one. Here are three admissible heuristics which correspond to a particular distance between the node of evaluation and the target node :

  • Euclidean distance > Sqrt(Dx²+Dy²+Dz²)
  • Manhattan distance > |Dx|+|Dy|+|Dz|
  • Maximum distance > Max(|Dx|, |Dy|, |Dz|)

These functions give an estimation of the remaining distance for each node that can be explored. Thus the search can be oriented toward the 'best' nodes. For a given node, the sum [Current cost + Heuristic value] is an estimation of the cost of reaching the ending node from the starting node, passing by the current one. This value is used to continuously choose the most promising path.

In practice, the algorithm maintains 2 lists of nodes that are filled and modified during the search :

  1. The first one, called Open, contains the tracks leading to nodes that can be explored. Initially, there is only the starting node. At each step, the best node of Open is taken out. Then, the best successor of this node (according to the heuristic) is added to the list as a new track. One doesn't know where the nodes that are in Open lead, because they have not been propagated yet. However, the best one is examined at each new step.
  2. The second one, called Closed, stores the tracks leading to nodes that have already been explored.

The program is based on a recursive model. The loop is performed as long as Open still contains some elements. See the code for details (written with C#, it is sufficient enough to be self explainable).

public bool SearchPath(Node StartNode, Node EndNode)
{
    lock (_Graph)
    {
        Initialize(StartNode, EndNode);
        while ( NextStep() ) {}
        return PathFound;
    }
}

Using the code: public interfaces of the component

The Graph class gathers a set of methods to manage its data, such as :

  • Add/Suppress a node/arc
  • Get the nearest/farthest node/arc from a point
  • Activate/Inactivate the entire graph
  • Empty the graph

From a more practical point of view, here are the methods and properties you can use for the main classes and objects :

public class Graph
{
    public Graph()

    public IList Nodes { get }
    public IList Arcs { get }

    public void Clear()
    public bool AddNode(Node NewNode)
    public Node AddNode(float x, float y, float z)
    public bool AddArc(Arc NewArc)
    public Arc AddArc(Node StartNode, Node EndNode, float Weight)
    public void Add2Arcs(Node Node1, Node Node2, float Weight)
    public bool RemoveNode(Node NodeToRemove)
    public bool RemoveArc(Arc ArcToRemove)
    public void BoundingBox(out double[] MinPt, out double[] MaxPt)
    public Node ClosestNode(double X, double Y, double Z, out double Dist, <BR>                            bool IgnoreFreeWay)
    public Arc ClosestArc(double X, double Y, double Z, out double Dist, <BR>                            bool IgnoreFreeWay)
}

public class Node
{
    public Node(double PositionX, double PositionY, double PositionZ)

    public IList IncomingArcs { get }
    public IList OutgoingArcs { get }
    public double X { get }
    public double Y { get }
    public double Z { get }
    public void ChangeXYZ(double PositionX, double PositionY, <BR>                          double PositionZ)
    public bool Passable { get/set }

    public Node[] AccessibleNodes { get }
    public Node[] AccessingNodes { get }
    public void UntieIncomingArcs()
    public void UntieOutgoingArcs()
    public void Isolate()
    public Arc ArcGoingTo(Node N)
    public Arc ArcComingFrom(Node N)
    public object Clone()

    public static void BoundingBox(IList NodesGroup, out double[] MinPt, <BR>                                   out double[] MaxPt)
    public static double EuclidianDistance(Node N1, Node N2)
    public static double SquareEuclidianDistance(Node N1, Node N2)
    public static double ManhattanDistance(Node N1, Node N2)
    public static double MaxDistanceAlongAxis(Node N1, Node N2)

    public override string ToString()
    public override bool Equals(object O)
    public override int GetHashCode()
}

public class Arc
{
    public Arc(Node Start, Node End)

    public Node StartNode { get/set }
    public Node EndNode { get/set }
    public double Weight { get/set }
    public bool Passable { get/set }

    virtual public double Cost { get }
    public double Length { get }
    virtual protected double CalculateLength()

    public override string ToString()
    public override bool Equals(object O)
    public override int GetHashCode()
}

public class AStar
{
    public AStar(Graph G)

    public bool SearchPath(Node StartNode, Node EndNode)
    public void Initialize(Node StartNode, Node EndNode)
    public bool NextStep()

    public bool Initialized { get }
    public int StepCounter { get }
    public bool SearchStarted { get }
    public bool SearchEnded { get }
    public bool PathFound { get }
    public Node[] PathByNodes { get }
    public Arc[] PathByArcs { get }
    public bool ResultInformation(out int NbArcsOfPath, out double CostOfPath)

    public double DijkstraHeuristicBalance { get/set }
    public Heuristic ChoosenHeuristic { get/set }
    public static Heuristic EuclidianHeuristic { get }
    public static Heuristic MaxAlongAxisHeuristic { get }
    public static Heuristic ManhattanHeuristic { get }
}

Nodes and arcs can be Passable or not. Selecting one of these two states for a node, propagates the same state to outgoing and incoming arcs. Likewise, destroying a node implies the destruction of all the directly linked arcs. In addition, an arc must always be linked to two nodes, whereas a node can be isolated.

Note that these data override Object.ToString() as well as Object.Equals(Object O). Moreover they can be serialized.

Simple example of the usage with the console application

using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using EMK.Cartography;

namespace EMK.Tests
{
    public class TestAStar
    {
        public static void Main()
        {
            try
            {
                Graph G = new Graph();
                Node N1 = G.AddNode(0,0,0);
                Node N2 = G.AddNode(5,0,0);
                Node N3 = G.AddNode(5,5,0);
                Node N4 = G.AddNode(5,5,5);
                G.AddArc(N1,N2,1);
                G.AddArc(N2,N3,1);
                G.AddArc(N3,N4,1);
                G.AddArc(N1,N3,1);

                Console.WriteLine( ListNodesAndArcs(G) );
                Console.WriteLine("Best path to reach "+N4+" from "+N1+" :");
                AStar AS = new AStar(G);
                if ( AS.SearchPath(N1, N4) )
                    foreach (Arc A in AS.PathByArcs) 
                        Console.WriteLine( A.ToString() );
                else Console.WriteLine( "No result !" );

                Console.Write("Serialize and Deserialize. ");
                Stream StreamWrite = File.Create("GraphSaved.bin");
                BinaryFormatter BinaryWrite = new BinaryFormatter();
                BinaryWrite.Serialize(StreamWrite, G);
                StreamWrite.Close();

                Stream StreamRead = File.OpenRead("GraphSaved.bin");
                BinaryFormatter BinaryRead = new BinaryFormatter();
                Graph G2 = (Graph) BinaryRead.Deserialize(StreamRead);
                StreamRead.Close();
                Console.WriteLine( ListNodesAndArcs(G2) );
            }
            catch(Exception e) 
            { 
                Console.Write( "Error :\n\n"+e.ToString() ); 
            }
        }

        static private string ListNodesAndArcs(Graph GraphToDescribe)
        {
            StringBuilder SB = new 
                StringBuilder("Description of the Graph:\n\tNodes> ");
            foreach (Node N in GraphToDescribe.Nodes) 
                SB.Append( N.ToString()+"; " );
            SB.Append("\n\tArcs> ");
            foreach (Arc A in GraphToDescribe.Arcs) 
                SB.Append( A.ToString()+"; " );
            return SB.ToString();
        }
    }
}
Description of the Graph:
        Nodes> {0;0;0}; {5;0;0}; {5;5;0}; {5;5;5}
        Arcs> {0;0;0}-->{5;0;0}; {5;0;0}-->{5;5;0}; 
                    {5;5;0}-->{5;5;5}; {0;0;0}-->{5;5;0}
Best path to reach {5;5;5} from {0;0;0} :
{0;0;0}-->{5;5;0}
{5;5;0}-->{5;5;5}
Serialize and Deserialize. Description of the Graph:
        Nodes> {0;0;0}; {5;0;0}; {5;5;0}; {5;5;5}
        Arcs> {0;0;0}-->{5;0;0}; {5;0;0}-->{5;5;0}; 
                    {5;5;0}-->{5;5;5}; {0;0;0}-->{5;5;0}

Example of use in a graphical environment

The graphical interface aims at bringing the component into play so as to reflect, fairly and simply, what it can do. The application lets you draw, move, erase or inactivate several nodes and arcs. When your graph is complete you just have to click on the 'A*' icon and select the starting and ending nodes with the respective left and right mouse buttons. Then you will automatically see the best way. If you modify the graph, this path will be updated.

If you want to visualize the algorithm's logic, then select the 'Step by Step' mode in the sub-menu of the 'A*' icon. The idea is to give the user a clear view of what happens.

Using step by step mode

In the option panel ('?' icon), you can change the heuristic and set its influence. The min value (in fact 0) means that only the heuristic will be used. On the contrary, the max value (in fact 1) means that the search will behave in accordance with the Dijkstra algorithm.

Options panel

Remarks

The program has been validated with a series of adapted tests, gathering various scenarios and interactions. Nevertheless it does not aim to be a perfect application, but an unassuming program that modestly claims to be simple and ergonomic. For information, I had already done this work with C++ for the DLL as well as MFC for the 2D graphical interface. I had also tested it in a 3D context with an OpenGL graphical interface. The next step will be to implement the 3D interface with Direct3D 9.0 for .NET or with one of the OpenGL solutions for this framework.

The source code contains a light but interesting implementation of geometrical tools such as Point3D and Vector3D. They were needed for graphical interactions in the GraphFormer example application (projections on lines, vectors' operations, etc...).

For convenience and performance I used the SortableList type for Closed and Open lists. The advantage is that, the best track will always be the first element, since the list remains sorted (cf the article I have written formerly about Automatic Sorted List). Nonetheless it would be very easy to replace it with another list type, such as ArrayList, provided you look for the 'best' element (that is to say the lower) at each step.

History

  • June the 22nd, 2003 : Translation of the library code and comments from French to English.
  • June the 24th, 2003 : Article submission.
  • June the 26th, 2003 : No more Flickering thanks to DoubleBuffer Panel style.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Eric Marchesin
Web Developer
France France
Eric Marchesin is working as a software development engineer at Dassault Systèmes, Paris. Dassault Systèmes is a global leader in the market for Product Lifecycle Management using 3D modeling digital technology.
He has also been working for Video Game companies as well as Artificial Intelligence projects.
His programming experience more specifically includes C/C++, MFC, OpenGL, C# and .NET framework.
 
Of course I appreciate beautiful and fine algorithms. There's quite an art to creating powerful, effective and ergonomic programs.
Among many other things I enjoy music, sun, passion and generally whatever makes you imagine, travel and dream.

Comments and Discussions

 
Questionthanks Pinmembercumthyb16-Mar-14 2:27 
QuestionRe: Great sample!! PinmemberMahsa Hassankashi2-Mar-14 3:04 
QuestionSmall bug PinmemberMember 1046794015-Dec-13 11:25 
QuestionPlease fix rendering of power of 2 PinmemberSAKryukov3-Jul-11 12:48 
Generalhi Pinmembernghiemvanhung10-Jun-11 6:06 
Generalcustom heuristic PinmemberAsafMeir20-Feb-11 1:55 
Questionsorting nodes Pinmemberghada_ahmed17-Jul-10 2:25 
Generaleuclidean distance Pingroupmetyouba28-Mar-10 21:18 
QuestionHow to set and display labels for nodes and arcs? Pinmemberpluto.plutone23-Jul-09 3:30 
GeneralSomewhat naive... Pinmemberpetervn22-Jul-09 12:07 
QuestionNeed help on C# Programming, Pinmembernosa oko21-Nov-08 21:26 
GeneralFaster PinmemberKilpo21-Oct-08 19:14 
GeneralRe: Faster [modified] Pinmemberchenyingchun9-Jul-11 22:39 
GeneralWell done PinmemberFco. Javier Marin20-Oct-08 15:13 
Questionhow to input edges pair?? Pinmemberfarhancrazy5-Sep-08 7:14 
Question2D Cut optimization software PinmemberMember 36930339-May-08 11:12 
Generallicense type PinmemberRul5-Mar-08 0:46 
AnswerRe: license type PinmemberLeinir Turthra19-Nov-08 5:21 
Questioneric marchesin, are you dead??? Pinmembermurathankocan2-Mar-08 7:06 
Questionis it possible, for OCR? Pinmemberreymond18-Jan-08 14:51 
GeneralAnother method... Pinmembercplas7-Jan-08 19:56 
GeneralRe: Another method... Pinmembermurathankocan11-Jan-08 22:00 
GeneralRe: Another method... Pinmembercplas12-Jan-08 9:52 
GeneralRe: Another method... PinmemberMember 47587035-Feb-08 16:14 
GeneralPlease help me for Senoir Project PinmemberSalmaAlnajim7-Jan-08 8:04 
Generalgrabbing data from sql table Pinmembermurathankocan4-Jan-08 21:59 
QuestionAdding more then 4 Nodes ? PinmemberSTF-DIR26-Dec-07 18:54 
AnswerRe: Adding more then 4 Nodes ? PinmemberMaddix8-Jan-08 22:36 
GeneralRe: Adding more then 4 Nodes ? PinmemberSTF-DIR8-Jan-08 23:50 
GeneralGreat code! Pinmembereduardosl11-Dec-07 12:56 
Generalquestion Pinmembercacu20-Aug-07 7:10 
GeneralRe: question PinmemberZenyukIV13-Sep-07 19:43 
GeneralRe: question Pinmembercacu14-Sep-07 2:52 
GeneralBravo PinmemberJay Gatsby18-Aug-07 13:24 
GeneralA Challenge! PinmemberEderAndres31-May-07 6:45 
Questionhow can i compile this? Pinmemberkaksx19-Nov-06 14:05 
Generalhelping out me from my final year project submission [modified] PinmemberAsif Ud-din Ahmed24-Oct-06 21:01 
GeneralRe: helping out me from my final year project submission Pinmemberazam's25-Feb-07 17:57 
Generalplz help!!! Pinmemberrupalini_rupal22-Oct-06 7:39 
GeneralRe: plz help!!! Pinmemberazam's25-Feb-07 18:00 
QuestionPath optimisation. PinmemberWestves21-Jun-06 23:23 
GeneralRe: Path optimisation. Pinmembercplas12-Jan-08 9:54 
GeneralA* Help PinmemberDusty Young9-Apr-06 19:24 
GeneralRe: A* Help PinmemberRichard-SP6-Feb-07 2:41 
QuestionDijkstraHeuristicBalance= 0 means Dijkstra or pure Heuristic? PinmemberChean Chung8-Apr-06 19:25 
Dear Mr Eric,
In your article, you've mentioned that:
 
"The min value (in fact 0) means that only the heuristic will be used. On the contrary, the max value (in fact 1) means that the search will behave in accordance with the Dijkstra algorithm."
However, in the comments of your code (Astar.cs), you wrote:
 
This value must belong to [0; 1] and it determines the influence of the heuristic on the algorithm.
/// If this influence value is set to 0, then the search will behave in accordance with the Dijkstra algorithm.
/// If this value is set to 1, then the cost to come to the current node will not be used whereas only the heuristic will be taken into account.

 
Moreover, another explanation is found in track.cs:
 
-> 0 will minimize the number of nodes explored but will not take the real cost into account.
-> 0.5 will minimize the cost without developing more nodes than necessary.
-> 1 will only consider the real cost without estimating the remaining cost.");

 
They made me confused. Therefore, can you please clarify whether 0 indicates a Dijkstra or a pure Heuristics? Thanks.
Questionlovely work! Any interface or class for data binding? Pinmemberchewik3-Apr-06 19:06 
GeneralClosed List Pinmemberjcuglo7-Feb-06 6:59 
GeneralGood Article PinmemberRohit Wason6-Feb-06 8:57 
Questionneed help Pinmemberlily_km28-Oct-05 21:50 
QuestionObject Oriented Programming! PinsussMohammad Reza Kamyab22-Oct-05 21:30 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 24 Jun 2003
Article Copyright 2003 by Eric Marchesin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid