DijkstraGraph - An Apex Class For Dijkstra's Shortest Path Algorithm

No alt text provided for this image

I have created a parent class DijkstraGraph with two main child classes Vertex and Edge.

Class vertex has two properties:

1. id - Identifier of vertex

2. adjacentVertices - Set of ids of adjacent vertices in outward direction

Class Edge has three properties:

1. beginVertex - Id of begin vertex of the edge

2. endVertex - Id of end vertex of the edge

3. cost - Cost of the edge

Begin vertex and end vertex together give edge identifier.

Class DijkstraGraph has two properties:

1. vertices - Hashmap of vertices

2. edges - Hasmap of edges

It has following methods:

1. addVertex - To add specified vertex to the graph

2. removeVertex - To remove specified vertex from the graph

3. addEdge - To add an edge in the graph, connecting the specified vertices with specified cost. An external id also can be specified, if any. Also, direction of edge can be specified too. If it is not directed, edges will be created in two directions with the specified vertices with same cost.

4. removeEdge - To remove edge in the specified direction between the specified vertices, from the graph. If there is no direction, edges in both direction between the vertices. will be removed.

5. updateEdgeCost - To update the cost of an edge connecting the two vertices.

6. getVertexIds - To get the set of all vertex ids in the graph

7.getEdgeIds - To get the set of all edge ids in the graph

8. initVisitInformation - To initialize the visit details of the edge before finding the optimal path between the specified source and target vertices. Setting cheapest cost to Infinity for all vertices except source is the main initialization. For, it must be set to zero.

9. addPathExclusions - To specify consecutive vertices of a path to be excluded during the cheapest path finding.

10. addPointExclusions - To specify one or more vertices to be excluded during the cheapest path finding.

11. addViaPoints - To assign high priority to the specified vertices. The final path must pass through those vertices irrespective of the cost.

12. getOptimalPathVerticesWithCheapestCost - To find the cheapest optimal path between a source and target vertices after considering all exclusions and via points.


Mate, you lost me a long time ago - but well done for this achievement!

Like
Reply

To view or add a comment, sign in

More articles by Shabu Thomas

Others also viewed

Explore content categories