Generic graph implementations (unweighted and weighted, directed or undirected) with three classic traversal/shortest-path algorithms. The demo in Main models a small map of Kazakhstani cities and finds routes using all three algorithms.
Vertex<V>— graph node storing typed data and an adjacency map(Vertex → weight). Uses a static counter for a stablehashCode.Edge<V>— weighted directed edge with source, destination, anddoubleweight.MyGraph<V>— adjacency-list graph (directed or undirected) for unweighted edges. Backed by aHashMap<V, Vertex<V>>.WeightedGraph<V>— same structure extended for weighted edges;addEdgeaccepts adoubleweight.Search<V>— abstract base holdingmarked(visited set) andedgeTo(path map); provideshasPathToandpathTo(reconstructs path by walkingedgeTo).BreadthFirstSearch<V>— BFS using aQueue; visits vertices level-by-level; time O(V+E).DepthFirstSearch<V>— recursive DFS; explores as deep as possible before backtracking; time O(V+E).Dijkstra<V>— greedy shortest-path on aWeightedGraph; uses an unsettled-node set for relaxation; time O(V²) with linear scan (noted in Javadoc as improvable to O(E + V log V) with a priority queue).
src/
├── Main.java # Demo: Almaty → Kyzylorda via Dijkstra, DFS, BFS
└── graphs/
├── Vertex.java # Graph node with weighted adjacency map
├── Edge.java # Weighted directed edge
├── MyGraph.java # Unweighted adjacency-list graph
├── WeightedGraph.java # Weighted adjacency-list graph
├── Search.java # Abstract base: marked, edgeTo, pathTo
├── BreadthFirstSearch.java # BFS traversal
├── DepthFirstSearch.java # DFS traversal
└── Dijkstra.java # Dijkstra's shortest-path algorithm
javac -d out src/graphs/*.java src/Main.java
java -cp out MainSample output:
Dijkstra:
Almaty -> Astana -> Kyzylorda ->
DFS:
Almaty -> Shymkent -> Kyzylorda ->
BFS:
Almaty -> Shymkent -> Kyzylorda ->
Adil Ormanov — GitHub