Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将等价图合并在一起_Java_Algorithm_Data Structures_Hash_Graph - Fatal编程技术网

Java 将等价图合并在一起

Java 将等价图合并在一起,java,algorithm,data-structures,hash,graph,Java,Algorithm,Data Structures,Hash,Graph,我不明白如何使两个图形合并在一起。见下图: 这里我想合并两个图,根是A和E。由于E较小,我想让它指向较大的图A的根。我有一个名为compressoroot()的函数,它接受一个参数,使其自身和根之间的每个节点指向自参考根(例如,compressoroot(D)):D->A,C->A,B->A)。如果我有这个功能 public Equivalence<E> mergeClassesContaining(E a, E b); 如何完成mergeClassesContaining(ea

我不明白如何使两个图形合并在一起。见下图:

这里我想合并两个图,根是A和E。由于E较小,我想让它指向较大的图A的根。我有一个名为
compressoroot()
的函数,它接受一个参数,使其自身和根之间的每个节点指向自参考根(例如,compressoroot(D)):D->A,C->A,B->A)。如果我有这个功能

public Equivalence<E> mergeClassesContaining(E a, E b);
如何完成
mergeClassesContaining(ea,eb)
函数,以便它确定哪个是较小的图形,并将其根指向较大的图形?在这种情况下,图形根据其“节点”的数量而变大或变小。例如,左侧有4个,右侧有2个

public Equivalence<E> mergeClassesContaining(E a, E b) throws IllegalArgumentException {


    if (!inSameClass(a, b)) { //private function to determine if the given parameters are in the same class.
        //need help here.
        return this;
    }
}
公共等价合并类包含(EA,EB)抛出IllegalArgumentException{
if(!inSameClass(a,b)){//private函数来确定给定参数是否在同一个类中。
//这里需要帮助。
归还这个;
}
}
以下是CompressorRoot函数:

private E compressToRoot (E e) throws IllegalArgumentException {
   E node;
   ArrayList<E> nodes = new ArrayList<E>();
    while ((node = parentMap.get(e)) != e)  {
        nodes.add(e);
        e = node;
    }

    for (E element : nodes)
        ((ArrayList<E>) parentMap).set((Integer) node, e);

    return e; 
}
private E compressoroot(E)抛出IllegalArgumentException{
E节点;
ArrayList节点=新的ArrayList();
while((node=parentMap.get(e))!=e){
节点。添加(e);
e=节点;
}
对于(E元素:节点)
((ArrayList)parentMap.set((Integer)节点,e);
返回e;
}
完整代码:

public class HashEquivalence<E> implements Equivalence<E>  {

public Equivalence<E> addSingletonClass (E e) throws IllegalArgumentException {
  if (parentMap.containsKey(e))
    throw new IllegalArgumentException("HashEquivalence.addSingletonClass: e(" +e+ ") already in an equivalence class");
  parentMap.put(e,e);    //its own parent
  rootSizeMap.put(e,1);  //its equivalence class has 1 value in it
  return this;
}

private E compressToRoot (E e) throws IllegalArgumentException {

   E node;
   ArrayList<E> nodes = new ArrayList<E>();
    while ((node = parentMap.get(e)) != e)  {
        nodes.add(e);
        e = node;
    }

    for (E element : nodes)
        ((ArrayList<E>) parentMap).set((Integer) node, e);

    return e; //Allows method to compile
}

public boolean inSameClass(E a, E b) throws IllegalArgumentException {
  if (!parentMap.containsKey(a))
    throw new IllegalArgumentException("HashEquivalence.inSameClass: a(" +a+ ") not in an equivalence class");
  if (!parentMap.containsKey(b))
    throw new IllegalArgumentException("HashEquivalence.inSameClass: b(" +b+ ") not in an equivalence class");

  return compressToRoot(a) == compressToRoot(b);
}


public Equivalence<E> mergeClassesContaining(E a, E b) throws IllegalArgumentException {


  if (!inSameClass(a,b))

return this; 
  }


public Set<Set<E>> allClasses () {
  Map<E,Set<E>> answerMap = new HashMap<E,Set<E>>();
  for (E e : parentMap.keys()) {
    E root = compressToRoot(e);
    Set<E> s = answerMap.get(root);
    if (s == null)
      answerMap.put(root, s = new HashSet<E>());
    s.add(e);
  }

return new HashSet<Set<E>>(1.0,answerMap.values());
}


public int numberOfClasses ()
{return rootSizeMap.size();}


public int numberOfMembers ()
{return parentMap.size();}


private Map<E,Integer> heights () {
  Map<E,Integer> answer = new HashMap<E,Integer>();
  for (E element : parentMap.keys()) {
    E e = element;
    int depth = 0;
    while (parentMap.get(e) != e) {
      e = parentMap.get(e);
      depth++;
    }
    Integer soFar = answer.get(e);
    if (soFar == null || soFar < depth)
      answer.put(e, depth);
  }
  return answer;
}


public int maxHeight () {
  return Collections.max(heights().values());
}


public void showMaps() {
  System.out.println("parentMap   (as list) = " + new ArrayList<Map.Entry<E,E>>(parentMap.entries()));
  System.out.println("rootSizeMap (as list) = " + new ArrayList<Map.Entry<E,Integer>>(rootSizeMap.entries()));
  System.out.println("heightMap   (as list) = " + new ArrayList<Map.Entry<E,Integer>>(heights().entries()));
  System.out.println("max height of tree    = " + maxHeight());
}


private Map<E,E>       parentMap   = new HashMap<E,E>();
private Map<E,Integer> rootSizeMap = new HashMap<E,Integer>();
}
公共类hashequality实现了等价性{
公共等价addSingletonClass(E)抛出IllegalArgumentException{
if(parentMap.containsKey(e))
抛出新的IllegalArgumentException(“HashEquivalence.addSingletonClass:e(“+e+”)已在等价类中”);
parentMap.put(e,e);//它自己的父对象
rootSizeMap.put(e,1);//其等价类中有1个值
归还这个;
}
private E CompressorRoot(E E)抛出IllegalArgumentException{
E节点;
ArrayList节点=新的ArrayList();
while((node=parentMap.get(e))!=e){
节点。添加(e);
e=节点;
}
对于(E元素:节点)
((ArrayList)parentMap.set((Integer)节点,e);
返回e;//允许方法编译
}
公共布尔inSameClass(ea,eb)抛出IllegalArgumentException{
如果(!parentMap.containsKey(a))
抛出新的IllegalArgumentException(“HashEquivalence.inSameClass:a(“+a+”)不在等价类中”);
如果(!parentMap.containsKey(b))
抛出新的IllegalArgumentException(“HashEquivalence.inSameClass:b(“+b+”)不在等价类中”);
返回压缩机启动(a)=压缩机启动(b);
}
公共等价合并类包含(EA,EB)抛出IllegalArgumentException{
如果(!inSameClass(a,b))
归还这个;
}
公共集合所有类(){
Map answerMap=新HashMap();
对于(E:parentMap.keys()){
E根=压缩机根(E);
Set s=answerMap.get(根);
如果(s==null)
put(root,s=newhashset());
s、 加(e);
}
返回新的HashSet(1.0,answerMap.values());
}
公共int numberOfClasses()
{返回rootSizeMap.size();}
公共int numberOfMembers()
{返回parentMap.size();}
私人地图高度(){
Map answer=new HashMap();
对于(E元素:parentMap.keys()){
E=元素;
int深度=0;
while(parentMap.get(e)!=e){
e=parentMap.get(e);
深度++;
}
整数soFar=answer.get(e);
if(soFar==null | | soFar
什么是“小班”?你在问题中遗漏了很多假设和背景。如果没有这些丢失的信息,它几乎是没有意义的。所以…第二步中的是一个不相交的图。你能解释一下这两个不相交的图是如何相互关联的吗?@user1766888诸如此类,你的实现是如此困难和错误……嗯,这是我在聊天中说的:所以,paste.org.ru/?2j783h
public class HashEquivalence<E> implements Equivalence<E>  {

public Equivalence<E> addSingletonClass (E e) throws IllegalArgumentException {
  if (parentMap.containsKey(e))
    throw new IllegalArgumentException("HashEquivalence.addSingletonClass: e(" +e+ ") already in an equivalence class");
  parentMap.put(e,e);    //its own parent
  rootSizeMap.put(e,1);  //its equivalence class has 1 value in it
  return this;
}

private E compressToRoot (E e) throws IllegalArgumentException {

   E node;
   ArrayList<E> nodes = new ArrayList<E>();
    while ((node = parentMap.get(e)) != e)  {
        nodes.add(e);
        e = node;
    }

    for (E element : nodes)
        ((ArrayList<E>) parentMap).set((Integer) node, e);

    return e; //Allows method to compile
}

public boolean inSameClass(E a, E b) throws IllegalArgumentException {
  if (!parentMap.containsKey(a))
    throw new IllegalArgumentException("HashEquivalence.inSameClass: a(" +a+ ") not in an equivalence class");
  if (!parentMap.containsKey(b))
    throw new IllegalArgumentException("HashEquivalence.inSameClass: b(" +b+ ") not in an equivalence class");

  return compressToRoot(a) == compressToRoot(b);
}


public Equivalence<E> mergeClassesContaining(E a, E b) throws IllegalArgumentException {


  if (!inSameClass(a,b))

return this; 
  }


public Set<Set<E>> allClasses () {
  Map<E,Set<E>> answerMap = new HashMap<E,Set<E>>();
  for (E e : parentMap.keys()) {
    E root = compressToRoot(e);
    Set<E> s = answerMap.get(root);
    if (s == null)
      answerMap.put(root, s = new HashSet<E>());
    s.add(e);
  }

return new HashSet<Set<E>>(1.0,answerMap.values());
}


public int numberOfClasses ()
{return rootSizeMap.size();}


public int numberOfMembers ()
{return parentMap.size();}


private Map<E,Integer> heights () {
  Map<E,Integer> answer = new HashMap<E,Integer>();
  for (E element : parentMap.keys()) {
    E e = element;
    int depth = 0;
    while (parentMap.get(e) != e) {
      e = parentMap.get(e);
      depth++;
    }
    Integer soFar = answer.get(e);
    if (soFar == null || soFar < depth)
      answer.put(e, depth);
  }
  return answer;
}


public int maxHeight () {
  return Collections.max(heights().values());
}


public void showMaps() {
  System.out.println("parentMap   (as list) = " + new ArrayList<Map.Entry<E,E>>(parentMap.entries()));
  System.out.println("rootSizeMap (as list) = " + new ArrayList<Map.Entry<E,Integer>>(rootSizeMap.entries()));
  System.out.println("heightMap   (as list) = " + new ArrayList<Map.Entry<E,Integer>>(heights().entries()));
  System.out.println("max height of tree    = " + maxHeight());
}


private Map<E,E>       parentMap   = new HashMap<E,E>();
private Map<E,Integer> rootSizeMap = new HashMap<E,Integer>();
}