From 79e04705c6eeed95992e5753a8328aad90e02f68 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 20 Apr 2025 19:39:42 +0200 Subject: move each auxiliary class to own file, to please javadoc --- com/example/portfolio3/MatrixGraph.java | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 com/example/portfolio3/MatrixGraph.java (limited to 'com/example/portfolio3/MatrixGraph.java') diff --git a/com/example/portfolio3/MatrixGraph.java b/com/example/portfolio3/MatrixGraph.java new file mode 100644 index 0000000..f3a5bde --- /dev/null +++ b/com/example/portfolio3/MatrixGraph.java @@ -0,0 +1,77 @@ +package com.example.portfolio3; + +// origin: + +import java.util.*; + +/// Matrix Graph: weights are stored in a twodimensional array +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 vertex2index=new HashMap<>(); + + /// foo + private int numVertex; // maximum number of vertices + + /// foo + /// @param numVertex maximum number of vertices allowed + 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 edges(){ + HashSet edges=new HashSet<>(); + for(int i=0;i outEdge(Vertex v1){ + HashSet edges=new HashSet<>(); + int i=vertex2index.get(v1); + for(int j=0;j