Übung Grundbegriffe der Graphentheorie 1
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>Java</language>
==Programmiertechnische Umsetzung von Graph-Strukturen==
*Gegeben ist folgendes IGraph Interface mit einer Hilfsklasse Edge zur einfacheren Behandlung von Kanten
<code id="562cd5b1df82b">
interface IGraph{
// provides vertices as numbers (ascending, 0-based)
// and edges between them (edges can be null if there is none)
void setGraph(int[] vertices, Edge[] edges);
// checks if IGraph returns correct vertices and edges
int[] getVertices(); // returns vertices ascending
Edge[] getEdges(); // returns edges
boolean isAdjacent(int v1, int v2); // returns whether v1 is connected to v2
int getDegree(int vertex); // returns the degree of a certain vertex
int getSize(); // returns the size of the class properties
}
class Edge{
public int v1;
public int v2;
public Edge(int v1, int v2){
// from smaller to greater for improved comparison
this.v1 = v1 < v2 ? v1 : v2;
this.v2 = v1 < v2 ? v2 : v1;
}
@Override
public int hashCode() {
return v1*31+v2;
}
@Override
public boolean equals(Object o){
if(o instanceof Edge)
{
Edge e = (Edge) o;
return (this.v1 == e.v1 && this.v2 == e.v2);
}
return false;
}
}
</code>