-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
julian
committed
Dec 3, 2024
1 parent
883c4c4
commit 7e0a720
Showing
21 changed files
with
443 additions
and
708 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 41 additions & 14 deletions
55
algorithm/src/main/java/com/example/algorithm/Graph/Graph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,58 @@ | ||
package com.example.algorithm.Graph; | ||
|
||
import lombok.Getter; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
|
||
@Getter | ||
public class Graph { | ||
protected GraphEdge[][] adjacencyMatrix; | ||
protected GraphNode[] vertexList; | ||
|
||
public Graph(String graphJSON) throws JSONException { | ||
JSONObject graph = new JSONObject(graphJSON); | ||
convJSONtoAdjacencyMatrix(graph.getJSONArray("edges")); | ||
constructFromJSONVertexList(graph.getJSONArray("vertices")); | ||
constructFromJSONEdgeList(graph.getJSONArray("edges")); | ||
} | ||
|
||
private void constructFromJSONVertexList(JSONArray jsonVertexList) throws JSONException { | ||
vertexList = new GraphNode[jsonVertexList.length()]; | ||
|
||
for (int i = 0; i < jsonVertexList.length(); i++) { | ||
JSONObject vertex = jsonVertexList.getJSONObject(i); | ||
|
||
vertexList[i] = new GraphNode(vertex); | ||
} | ||
} | ||
|
||
private void constructFromJSONEdgeList(JSONArray jsonEdgeList) throws JSONException { | ||
adjacencyMatrix = new GraphEdge[vertexList.length][vertexList.length]; | ||
|
||
for (int i=0; i < jsonEdgeList.length(); i++) { | ||
JSONObject edge = jsonEdgeList.getJSONObject(i); | ||
int from = getVertexId(edge.getString("from")); | ||
int to = getVertexId(edge.getString("to")); | ||
String id = edge.getString("id"); | ||
|
||
Double weight = null; | ||
if (edge.has("weight") && !edge.isNull("weight")) { | ||
weight = edge.getDouble("weight"); | ||
} | ||
|
||
adjacencyMatrix[from][to] = new GraphEdge(GraphEdge.UNVISITED, weight, id); | ||
} | ||
} | ||
|
||
private void convJSONtoAdjacencyMatrix(JSONArray adjMatrixJSON) throws JSONException { | ||
adjacencyMatrix = new GraphEdge[adjMatrixJSON.length()][adjMatrixJSON.length()]; | ||
for (int i = 0; i < adjMatrixJSON.length(); i++) { | ||
for (int j = 0; j < adjMatrixJSON.getJSONArray(i).length(); j++) { | ||
try { | ||
if (adjMatrixJSON.getJSONArray(i).getJSONObject(j).getString("weight").equals("null")) { | ||
adjacencyMatrix[i][j] = new GraphEdge(adjMatrixJSON.getJSONArray(i).getJSONObject(j).getInt("marking")); | ||
} else { | ||
adjacencyMatrix[i][j] = new GraphEdge(adjMatrixJSON.getJSONArray(i).getJSONObject(j).getInt("marking"), adjMatrixJSON.getJSONArray(i).getJSONObject(j).getDouble("weight")); | ||
} | ||
} catch (JSONException e) { | ||
adjacencyMatrix[i][j] = null; | ||
} | ||
public int getVertexId(String name) { | ||
for (int i = 0; i < vertexList.length; i++) { | ||
if (vertexList[i].getValue().equals(name)) { | ||
return i; | ||
} | ||
} | ||
|
||
throw new IndexOutOfBoundsException("Vertex not found"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.