/*
* Copyright (C) 2016 Naman Dwivedi
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package com.naman14.algovisualizer.algorithm.graph;
//Weighted graph implementation using adjacency matrix
public class WeightedGraph2 {
private int[][] edges; // adjacency matrix
private int[] labels;
public WeightedGraph2(int n) {
edges = new int[n][n];
labels = new int[n];
}
public int size() {
return labels.length;
}
public void setLabel(int vertex, int label) {
labels[vertex] = label;
}
public Object getLabel(int vertex) {
return labels[vertex];
}
public void addEdge(int source, int target, int w) {
edges[source][target] = w;
}
public boolean isEdge(int source, int target) {
return edges[source][target] > 0;
}
public void removeEdge(int source, int target) {
edges[source][target] = 0;
}
public int getWeight(int source, int target) {
return edges[source][target];
}
public int[] neighbors(int vertex) {
int count = 0;
for (int i = 0; i < edges[vertex].length; i++) {
if (edges[vertex][i] > 0) count++;
}
final int[] answer = new int[count];
count = 0;
for (int i = 0; i < edges[vertex].length; i++) {
if (edges[vertex][i] > 0) answer[count++] = i;
}
return answer;
}
}