简体   繁体   English

创建一个扩展Graph类的二部图。 需要一些指导

[英]Creating a Bipartite Graph that extends Graph class. Need some guidance

Looking for a step in the right direction. 寻找正确方向的一步。 I have with me 4 classes that I have made. 我参加了4堂课。 One is the super class which is graph and 3 subclasses called Edge, DirectedGraph, and BipartiteGraph. 一种是超类,它是图和3个子类,分别称为Edge,DirectedGraph和BipartiteGraph。

I am having some trouble with creating a bipartite graph. 我在创建二部图时遇到了一些麻烦。 Specifically I am given these directions: 具体来说,我得到了这些指示:

Extend the Graph class to create a new BipartiteGraph class. 扩展Graph类以创建一个新的BipartiteGraph类。 It should inherit all the functionality of the super class: 它应该继承超类的所有功能:

  • Automatically designate all even-index vertices (0,2,4) as part of the "A partition" from class and all odd-index vertices (1,3,5) as part of the "B partition". 自动从类中将所有偶数索引顶点(0,2,4)指定为“ A分区”的一部分,并将所有奇数索引顶点(1,3,5)指定为“ B分区”的一部分。 This requires no new code, just a conceptual expectation. 这不需要新的代码,只需要概念上的期望。

  • Override the constructor for Graph to have the same input (number of vertices), call the super constructor, and then verify the graph is bipartite. 重写Graph的构造函数,使其具有相同的输入(顶点数),调用super构造函数,然后验证图是否为二分图。 That is, make sure that all existing edges are from a vertex in A to a vertex in B. If the graph is not bipartite, wipe out the internal representation (eg, for an adjacency matrix, make a size 0x0 array) so it cannot be used! 也就是说,确保所有现有边都从A的顶点到B的顶点。如果图不是二分图,则擦除内部表示(例如,对于邻接矩阵,创建大小为0x0的数组),以便不能使用!

  • Add a method setPreferences() that takes as a parameter an integer and an array or ArrayList of integers. 添加一个方法setPreferences(),该方法将一个整数和一个整数数组或ArrayList作为参数。 The first integer is the vertex we want to attach preferences to and the list is that list of preferences, from most to least preferred. 第一个整数是我们要将偏好附加到的顶点,列表是偏好列表,从最喜欢到最不喜欢。 Verify that the array of ints contains all the members of the other partition in some order then save that information (you will need a 1-D array of arrays/ArrayLists to store these lists, one per vertex). 验证int数组以某种顺序包含另一个分区的所有成员,然后保存该信息(您将需要一个1-D的arrays / ArrayLists数组来存储这些列表,每个顶点一个)。

  • Add the method stableMatching that has no parameters and returns a stable matching (in the form of an ArrayList of Pairs of ints). 添加没有参数并返回稳定匹配(以成对的ArrayList形式)的方法stableMatching。 It will be helpful to consult Wikipedia: http://en.wikipedia.org/wiki/Stable_marriage_problem . 查阅Wikipedia将会很有帮助: http : //en.wikipedia.org/wiki/Stable_marriage_problem As a start, I suggest verifying that each vertex has a preference list set for it! 首先,我建议验证每个顶点都有一个首选列表!

Here is my constructor in the super class: 这是我在超类中的构造函数:

public class Graph {

// Setup privately modified variables which will define the graph

// These two parameters are storage variables for edges and vertices
// These variables were changed from Vertex and Edge to numVertices and
// numEdges.
private int numVertices;
private int numEdges;

// This will be the adjacency matrix to represent our graph, this will
// represent edges.
// adj_Matrix_Edges was previously static meaning it did not have access to
// multiple graphs, onyl one graph.
protected boolean[][] adj_Matrix_Edges;

// first step will be to setup the graph, using this constructor
public Graph(int vertices) {

    numVertices = vertices;

    if (numVertices < 0) {
        throw new RuntimeException(
                "Number of vertices cannot be a nonnegative value");
    }

    System.out.println("There are now " + numVertices
            + " vertices in the graph.");

    // A graph is created based on the specifications, N X N or (n^2)
    // graph.
    adj_Matrix_Edges = new boolean[vertices][vertices];
    }

And here is what I have so far for the BipartiteGraph class: 到目前为止,这是我对BipartiteGraph类的了解:

    public class BipartiteGraph extends Graph{

//Initialize two partitions for bipartite graph.
boolean[][] a;
boolean[][] b;


//Constructor of BipartiteGraph class
public BipartiteGraph(int vertices) {
    super(vertices);

    //Copy over even elements of graph into partition A.
    for (int i = 0; i < adj_Matrix_Edges.length; i++){
        for (int j = 0; j < adj_Matrix_Edges[i].length; j++){
            if (j%2 == 0){
                adj_Matrix_Edges[j] = a[j];
            }
        }
    }

    //Copy over odd elements of graph into Partition B.
    for (int i = 0; i < adj_Matrix_Edges.length; i++){
        for (int j = 0; j < adj_Matrix_Edges[i].length; j++){
            if (j%2 != 0){
                adj_Matrix_Edges[j] = b[j];
            }
        }
    }

}


public void setPreferences(int vertex, int[] preferences){

    if ()

}

public List stableMatching(){
    java.util.List<Integer> matching = new ArrayList<Integer>();


}

Am I making things too complicated, is the code simpler than it seems? 我使事情变得太复杂了吗,代码比看起来简单吗?

I think there is a mistake in the declaration of BipartiteGraph : 我认为BipartiteGraph的声明中有一个错误:

public class BipartiteGraph extends Graph{

boolean[][] a;
boolean[][] b;

You declare a and b as two dimensional arrays that is as matrices. 您将ab声明为作为矩阵的二维数组。 a and b models complementary subsets of the set of vertices. ab为顶点的互补子集建模。 Therefore, they should be either a list of vertices or an array of boolean which says if the ith vertex is in a . 因此,它们应该是一个顶点的列表布尔的阵列 ,其说,如果第i个顶点是在a Also you don't need to store both since one is the complementary to the other. 另外,您不需要存储两者,因为两者是互补的。

maybe I missed something, but do you have to write this as homework? 也许我错过了一些东西,但是您是否必须将其写为作业? otherwise, you could rely on JUNG and use HyperGraph: 否则,您可以依靠JUNG并使用HyperGraph:

http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/graph/Hypergraph.html http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/graph/Hypergraph.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM