diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 07:19:10 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 07:19:10 +0200 |
commit | 705b7a5a32793f7ed8a24b8b35afe3f9d49348be (patch) | |
tree | 7cffeb0de03a4638b87f04e01e7193eacbeaeede /src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java | |
parent | a5c3599d7bc7a9ef5583ad2d50a55975f030fbea (diff) |
tidy whitespace
Diffstat (limited to 'src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java')
-rw-r--r-- | src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java | 145 |
1 files changed, 77 insertions, 68 deletions
diff --git a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java index 985fcbb..5a1f327 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java +++ b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java @@ -7,74 +7,83 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; -/// Matrix Graph: weights are stored in a twodimensional array +/// 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(final int numVertex){ - this.numVertex=numVertex; - matrix =new Integer[numVertex][numVertex]; - index2vertex=new Vertex[numVertex]; - } - - /// foo - /// @param v vertex - /// @return int - private int getIndex(final 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(final Vertex v1, final Vertex v2, final 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(final 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(final Vertex v1, final Vertex v2){ - // constant time operation - return matrix[vertex2index.get(v1)][vertex2index.get(v2)];} + /// 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<>(); + + /// maximum number of vertices + private int numVertex; + + /// foo + /// + /// @param numVertex maximum number of vertices allowed + public MatrixGraph(final int numVertex) { + this.numVertex = numVertex; + matrix = new Integer[numVertex][numVertex]; + index2vertex = new Vertex[numVertex]; + } + + /// foo + /// + /// @param v vertex + /// @return int + private int getIndex(final 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(final Vertex v1, final Vertex v2, final 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(final 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(final Vertex v1, final Vertex v2) { + + // constant time operation + return matrix[vertex2index.get(v1)][vertex2index.get(v2)]; + } } |