aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-04-27 16:43:17 +0200
committerJonas Smedegaard <dr@jones.dk>2025-04-27 16:43:22 +0200
commit8535a9ca92539bf12ec00cac7a4e47be604f0283 (patch)
tree7a1fb082d5218e18a0186d97a8ed1f7dd87fd41f /src/main/java/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
parentd104247b8bcdb2a38b680ac54e7ceb2bba155c0e (diff)
simplify path structure
Diffstat (limited to 'src/main/java/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java')
-rw-r--r--src/main/java/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java77
1 files changed, 0 insertions, 77 deletions
diff --git a/src/main/java/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java b/src/main/java/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
deleted file mode 100644
index 29005b7..0000000
--- a/src/main/java/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java
+++ /dev/null
@@ -1,77 +0,0 @@
-package com.example.portfolio3;
-
-// origin: <https://moodle.ruc.dk/course/section.php?id=211877>
-
-import java.util.*;
-
-/// Matrix Graph: weights are stored in a twodimensional array
-public class MatrixGraph extends AbstractGraph {
-
- /// foo
- private Integer[][] matrix=null; // made in constructor
-
- /// foo
- // We must be able to map vertices to index in matrix and back again
- private Vertex[] index2vertex; // made in constructor
-
- /// foo
- private Map<Vertex,Integer> vertex2index=new HashMap<>();
-
- /// foo
- private int numVertex; // maximum number of vertices
-
- /// foo
- /// @param numVertex maximum number of vertices allowed
- public MatrixGraph(int numVertex){
- this.numVertex=numVertex;
- matrix =new Integer[numVertex][numVertex];
- index2vertex=new Vertex[numVertex];
- }
-
- /// foo
- /// @param v vertex
- /// @return int
- private int getIndex(Vertex v){
- if(vertex2index.containsKey(v)) return vertex2index.get(v);
- int index=vertex2index.size();
- if(index>=index2vertex.length)throw new RuntimeException("Too many vertices in graph");
- vertex2index.put(v,index);
- index2vertex[index]=v;
- return index;
- }
-
- /// foo
- public void insertEdge(Vertex v1,Vertex v2,int w){
- matrix[getIndex(v1)][getIndex(v2)] = w;
- }
-
- /// foo
- public Collection<Edge> edges(){
- HashSet<Edge> edges=new HashSet<>();
- for(int i=0;i<numVertex;i++){
- for(int j=0;j<numVertex;j++){
- Integer weight=matrix[i][j]; // may be null
- if(weight==null)continue;
- edges.add(new Edge(index2vertex[i],index2vertex[j],weight));
- }
- }
- return edges;
- }
-
- /// foo
- public Collection<Edge> outEdge(Vertex v1){
- HashSet<Edge> edges=new HashSet<>();
- int i=vertex2index.get(v1);
- for(int j=0;j<numVertex;j++){
- Integer weight=matrix[i][j]; // may be null
- if(weight==null)continue;
- edges.add(new Edge(v1,index2vertex[j],weight));
- }
- return edges;
- }
-
- /// foo
- public Integer getWeight(Vertex v1,Vertex v2){
- // constant time operation
- return matrix[vertex2index.get(v1)][vertex2index.get(v2)];}
-}