首页 > 编程知识 正文

玩转图论算法资源,编程求中位数算法

时间:2023-05-06 08:21:59 阅读:165074 作者:4521

展开ljdwx

求图论算法java实现packagetest

importjava.io.BufferedReader;

importjava.io.FileReader;

importjava.io.IOException;

importjava.util.ArrayList;

importjava.util.Comparator;

importjava.util.HashMap;

importjava.util.Map;

importjava.util.PriorityQueue;

importjava.util.Queue;

publicclassMinSpanningTree{

classEdge{//内部类定义边缘的数据结果

intu,v,weight;

}

MapnodeFather=newHashMap (;

intcnt=0,nodeCnt=0;

publicminspanningtree (字符串路径) {

try{

字符串str;

StringwjddystrArray;

wile((str=br.readline ) )!=null ) {

strarray=str.split((s ) );

edges.add(CNT,newEdge ) );

edges.get(CNT ).u=integer.parseint ) strarray[0];

edges.get(CNT ).v=integer.parseint ) strarray[1];

edges.get(CNT ).weight=integer.parseint ) strarray[2];

if (! node father.contains key (edges.get (CNT ).u ) }

nodefather.put(Edges.get(CNT ).u,Edges.get ).u ); //初始化,father[i]=i;

}

if (! node father.contains key (edges.get (CNT ).v ) }

nodefather.put(Edges.get(CNT ).v,Edges.get ).v );

}

}

br.close (;

}catch(ioexceptione ) {

e .打印任务跟踪(;

}

}

publicbooleanunion(intu,intv ) )//并行操作

INTA=find(u;

intb=find(v );

if(a!=b ) {

nodefather.put(a,b );

返回真;

}

返回假;

}

公共查找(intx )//检查操作

if(x!=nodefather.get(x ) }{

nodefather.put(x,find ) node father.get (x );

}

returnodefather.get(x;

}

publicarraylistgetminspanningtree (

queuefsqueue=newpriorityqueue (1000,//边缘e68a 843231愚人节231343134313032313333133331333313333133313333133333313333333333313333331333333331333333333133333313333333133333333333

新公司

公共公司(edgeedgeone,Edgekxdjc ) {

edge one.weightkxdjc.weight ) )。

返回1;

elseif(edgeone.weight

else

返回0;

}

);

for(inti=0; I

}

while (! FsQueue.isEmpty () ) /遍历每条边

EdgeEdge=FsQueue.poll (;

if(union(edge.u,Edge.v ) ) }

result.add(edge;

}else{

连续;

}

}

返回结果;

}

publicstaticvoidmain (stringwjdyargs ) {

for(inti=0; I

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。