Az alábbi letöltési lehetőségek közül választhatsz: (
segítség)
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 2 KB
package hu.krivan.szakkor.idegenvezetes;
import java.util.ArrayList;
import java.util.Hashtable;
@author
public class Graph {
private Hashtable<String, Integer> idByName;
private Hashtable<Integer, String> nameById;
private boolean[][] connections;
private int disconnectedNode = -1;
private int nodesCount;
private ArrayList<Integer> tacticalPoints = new ArrayList<Integer>();
public Graph(String[] nodes, String[] edges) {
nodesCount = nodes.length;
idByName = new Hashtable<String, Integer>(nodesCount);
nameById = new Hashtable<Integer, String>(nodesCount);
for (int i = 0; i < nodesCount; i++) {
idByName.put(nodes[i], i);
nameById.put(i, nodes[i]);
}
connections = new boolean[nodesCount][nodesCount];
for (int i = 0; i < nodesCount; i++) {
for (int j = 0; j < nodesCount; j++) {
connections[i][j] = false;
}
}
String[] tmp;
int a, b;
for (int i = 0; i < edges.length; i++) {
tmp = edges[i].split(" ");
a = idByName.get(tmp[0]);
b = idByName.get(tmp[1]);
connect(a, b);
}
disconnectOneByOne();
for( int i : tacticalPoints ) {
System.out.print( nameById.get(i) + " " );
}
System.out.println();
}
private void connect(int a, int b) {
connections[a][b] = true;
connections[b][a] = true;
}
private void disconnectOneByOne() {
for (int i = 0; i < nodesCount; i++) {
disconnectedNode = i;
if (!isConnected()) {
tacticalPoints.add(i);
}
}
}
private boolean isConnected() {
ArrayList<Integer> queue = new ArrayList<Integer>();
if (disconnectedNode == 0) {
queue.add(1);
} else {
queue.add(0);
}
int node;
boolean[] usedNodes = new boolean[nodesCount];
for( int i=0; i<nodesCount; i++ ) {
usedNodes[i] = false;
}
while( !queue.isEmpty() ) {
node = queue.remove(0);
if( !usedNodes[node] && (disconnectedNode != node) ) {
for( int i=0; i<nodesCount; i++ ) {
if( connections[node][i] ) {
queue.add(i);
}
}
}
usedNodes[node] = true;
}
for( int i=0; i<nodesCount; i++ ) {
if( !usedNodes[i] ) return false;
}
return true;
}
}