# shakibim/traveling-salesman-problem-tsp-with-java-swing architecture

> Architecture diagram of the GitHub repository shakibim/traveling-salesman-problem-tsp-with-java-swing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shakibim/traveling-salesman-problem-tsp-with-java-swing
- Repository: https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing

## Overview

1. **Repository shape**
   - This is a small **Java desktop application** built with **Maven**.
   - The entire application appears to live in a single source file: `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`.
   - The `target/` directory is just Maven build output and is not part of the runtime architecture.

2. **Primary boundary: Swing UI on the JVM**
   - The app is a **Java Swing** GUI, not a web app or service.
   - All user interaction happens in a desktop window: adding vertices, adding edges, auto-generating graphs, and triggering TSP solving.

3. **Single-process, in-memory application**
   - There is no evidence of a database, filesystem persistence, or external API integration.
   - Graph state, weights, and solution results are managed **in memory** and reset with the UI’s clear action.

4. **Core domain model: graph + TSP route state**
   - The application centers on an editable graph of cities/vertices and weighted edges.
   - It supports both **directed and undirected** graph behavior, based on the README examples.
   - The solver works on the current graph state rather than loading data from elsewhere.

5. **Graph editing subsystem**
   - Users can:
     - create vertices by clicking,
     - add edges manually,
     - enter weights,
     - and use **Auto Connect** to generate a complete or near-complete weighted graph automatically.
   - This subsystem is responsible for building the problem instance that the TSP algorithms consume.

6. **TSP algorithm subsystem**
   - Three solvers are implemented:
     - **Naive / brute force**: exhaustive search, optimal but factorial-time.
     - **Dynamic Programming**: memoized optimal solver for smaller to medium graphs.
     - **Greedy**: nearest-neighbor heuristic, fast but not guaranteed optimal.
   - These are the computational core of the app and are selected from the GUI.

7. **Result flow**
   - User selects a graph and an algorithm.
   - The solver computes a route and total cost.
   - The UI then displays the result visually and in a details view.
   - This is the main data flow: **UI input → graph state → algorithm execution → highlighted solution + cost display**.

8. **Visualization subsystem**
   - The app renders the graph and solution route directly in Swing.
   - It can emphasize the chosen path and optionally hide non-optimal edges.
   - The README’s screenshots suggest custom drawing of nodes, edges, and directional arrows.

9. **Performance/behavior tradeoff built into the architecture**
   - The app is intentionally designed to compare algorithms:
     - Naive and DP are used for correctness/optimality on small graphs.
     - Greedy is used for large graphs where exact methods become impractical.
   - Large inputs can freeze the UI when exact solvers are used, which implies the computations run on the same desktop thread flow rather than in a separate job system.

10. **No backend layering**
   - There is no separate service layer, persistence layer, or network boundary visible from the repo layout.
   - The architecture is effectively a **monolithic Swing application** with UI, model, rendering, and algorithms concentrated in one class/file.

11. **Build/tooling boundary**
   - Maven handles compilation and packaging.
   - The compiled classes under `target/classes` indicate a standard Java build pipeline, but no additional runtime infrastructure.

12. **How to diagram it**
   - The most accurate architecture diagram would show:
     - **Swing UI / event handlers**
     - **Graph editor state**
     - **TSP solver algorithms**
     - **Graph rendering/output panel**
   - All of these are internal components of one desktop process, with no external system dependencies.

Main entry points: [`TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java) (Graph editor), [`TSPSolver/target/classes/com/mycompany/tspsolver`](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/tree/main/TSPSolver/target/classes/com/mycompany/tspsolver) (Compiled app), [`TSPSolver/pom.xml`](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/pom.xml) (Maven build)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Desktop app"]
  node_ui["Swing UI<br/>desktop gui<br/>[TSPSolver.java]"]
  node_canvas["Graph view<br/>custom render<br/>[TSPSolver.java]"]
  node_editor[("Graph editor<br/>in-memory state<br/>[TSPSolver.java]")]
  node_input(("Mouse input<br/>event handling<br/>[TSPSolver.java]"))
  node_auto{{"Auto Connect<br/>graph generator<br/>[TSPSolver.java]"}}
  node_solverpick["Solver select<br/>algorithm choice<br/>[TSPSolver.java]"]
  node_bf["Naive search<br/>exhaustive solver<br/>[TSPSolver.java]"]
  node_dp["Dynamic prog<br/>optimal solver<br/>[TSPSolver.java]"]
  node_greedy["Greedy TSP<br/>heuristic solver<br/>[TSPSolver.java]"]
  node_result["Route result<br/>solution state<br/>[TSPSolver.java]"]
  node_details["Details view<br/>result display<br/>[TSPSolver.java]"]
  node_clear(("Clear graph<br/>reset action<br/>[TSPSolver.java]"))
end

subgraph group_build["Build"]
  node_maven["Maven build<br/>build tool<br/>[pom.xml]"]
  node_classes["Compiled app<br/>build output"]
end

node_input -->|"edit graph"| node_editor
node_input -->|"generate"| node_auto
node_auto -->|"populate"| node_editor
node_ui -->|"run solver"| node_solverpick
node_solverpick -->|"brute force"| node_bf
node_solverpick -->|"dynamic programming"| node_dp
node_solverpick -->|"heuristic"| node_greedy
node_editor -->|"graph input"| node_bf
node_editor -->|"graph input"| node_dp
node_editor -->|"graph input"| node_greedy
node_bf -->|"route + cost"| node_result
node_dp -->|"route + cost"| node_result
node_greedy -->|"route + cost"| node_result
node_result -->|"display"| node_details
node_result -->|"highlight"| node_canvas
node_editor -->|"render graph"| node_canvas
node_clear -->|"reset"| node_editor
node_clear -->|"clear result"| node_result
node_maven -->|"compile"| node_classes
node_classes -->|"run"| node_ui

click node_ui "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_canvas "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_editor "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_input "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_auto "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_solverpick "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_bf "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_dp "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_greedy "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_result "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_details "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_clear "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java"
click node_maven "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/pom.xml"
click node_classes "https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/tree/main/TSPSolver/target/classes/com/mycompany/tspsolver"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ui,node_canvas,node_editor,node_input,node_auto,node_solverpick,node_bf,node_dp,node_greedy,node_result,node_details,node_clear toneBlue
class node_maven,node_classes toneAmber
```

## Components

### Desktop app

Single-process Swing application runtime

- [Swing UI](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): desktop gui · Main window and user interactions for editing graphs and running solvers · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Graph view](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): custom render · Draws vertices, edges, arrows, and highlighted routes · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Graph editor](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): in-memory state · Holds the editable graph, vertices, edges, and weights · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Mouse input](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): event handling · Handles clicks and UI actions for creating and modifying the graph · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Auto Connect](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): graph generator · Builds dense or complete weighted graphs automatically · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Solver select](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): algorithm choice · Chooses brute force, dynamic programming, or greedy solving · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Naive search](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): exhaustive solver · Brute-force TSP solver for exact results on small graphs · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Dynamic prog](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): optimal solver · Memoized exact TSP solver for small to medium graphs · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Greedy TSP](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): heuristic solver · Nearest-neighbor style fast approximation solver · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Route result](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): solution state · Stores the chosen route, total cost, and highlighted path · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Details view](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): result display · Shows the computed route and cost to the user · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`
- [Clear graph](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java): reset action · Resets the current graph and solution state · `TSPSolver/src/main/java/com/mycompany/tspsolver/TSPSolver.java`

### Build

Maven compilation and packaged output

- [Maven build](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/blob/main/TSPSolver/pom.xml): build tool · Compiles and packages the Java application · `TSPSolver/pom.xml`
- [Compiled app](https://github.com/shakibim/traveling-salesman-problem-tsp-with-java-swing/tree/main/TSPSolver/target/classes/com/mycompany/tspsolver): build output · Generated class files under target/classes · `TSPSolver/target/classes/com/mycompany/tspsolver`

## Connections

- Mouse input → Graph editor: edit graph · User clicks create and connect vertices through the UI
- Mouse input → Auto Connect: generate · UI triggers automatic graph generation
- Auto Connect → Graph editor: populate · Generated vertices and weighted edges are written into the graph state
- Swing UI → Solver select: run solver · User selects an algorithm from the GUI
- Solver select → Naive search: brute force · Routes small graphs through exhaustive search
- Solver select → Dynamic prog: dynamic programming · Routes small and medium graphs through memoized exact solving
- Solver select → Greedy TSP: heuristic · Routes larger graphs through nearest-neighbor style solving
- Graph editor → Naive search: graph input · Exact solvers consume the current weighted graph
- Graph editor → Dynamic prog: graph input · Exact solvers consume the current weighted graph
- Graph editor → Greedy TSP: graph input · Heuristic solver consumes the current weighted graph
- Naive search → Route result: route + cost · Brute force returns the best tour and total weight
- Dynamic prog → Route result: route + cost · Dynamic programming returns the optimal tour and total weight
- Greedy TSP → Route result: route + cost · Greedy solver returns an approximate tour and total weight
- Route result → Details view: display · Solution summary is shown in the details panel
- Route result → Graph view: highlight · Chosen path is emphasized in the drawing surface
- Graph editor → Graph view: render graph · Current graph state is painted in Swing
- Clear graph → Graph editor: reset · Clears vertices, edges, and weights
- Clear graph → Route result: clear result · Removes the previous route and cost display
- Maven build → Compiled app: compile · Maven produces the compiled application classes
- Compiled app → Swing UI: run · Compiled classes contain the desktop application entrypoint
