Minimaler Schnitt wird hervorgehoben, wenn der Algorithmus fertig ist

main
Konrad Krauth 2024-11-27 14:50:11 +01:00
parent d7887f77db
commit eba13bb713
1 changed files with 32 additions and 4 deletions

View File

@ -18,6 +18,7 @@ editMode="drag";
draggedVertex=null;
possibleNextSteps=[];
flowVertices=["s"];
reachableFromStart=null;
function draw(){
function drawEdge(vertexFrom,vertexTo,number,fillColor,strokeColor){
co.fillStyle=fillColor;
@ -49,12 +50,24 @@ function draw(){
co.fillStyle="#888888"
co.fillRect(0,0,1000,1000);
for(let vertex of vertices){
let vertexReachable=false;
if(done){
for(let reachableOne of reachableFromStart){
if(reachableOne==vertex.name){
vertexReachable=true;
}
}
}
if(vertex.special=="source"){
co.fillStyle="#aaffaa";
}else if(vertex.special=="target"){
co.fillStyle="#ffaaaa";
}else{
co.fillStyle="white";
if(done){
co.fillStyle="#ffaaaa";
if(vertexReachable){co.fillStyle="#aaffaa"}
}
for(let alreadyVisited of flowVertices){
if(alreadyVisited==vertex.name){
co.fillStyle="#aaaaff";
@ -77,9 +90,23 @@ function draw(){
}
co.fillStyle="black";
co.font="10px sans-serif";
co.fillText(vertex.name,vertex.x,vertex.y,20);
for(let otherVertex of vertices){
if(vertexReachable){
otherVertexReachable=false;
for(let reachableOne of reachableFromStart){
if(reachableOne==otherVertex.name){
otherVertexReachable=true;
}
}
if(otherVertexReachable==false){
co.font="bold 15px sans-serif";
}
}
if(shownEdges=="capacity"){
if(edgesCapacity[vertex.name][otherVertex.name]>0){
drawEdge(vertex,otherVertex,edgesCapacity[vertex.name][otherVertex.name],"black","white");
@ -97,6 +124,7 @@ function draw(){
drawEdge(vertex,otherVertex,getEdge(edgesFlow,vertex.name,otherVertex.name)+"/"+edgesCapacity[vertex.name][otherVertex.name],"black",edgesFlow[vertex.name][otherVertex.name]>0?(edgesFlow[vertex.name][otherVertex.name]==edgesCapacity[vertex.name][otherVertex.name]?"#ff88ff":"#8888ff"):"white");
}
}
co.font="10px sans-serif";
}
}
for(let i=0;i<flowVertices.length-1;i++){
@ -209,7 +237,6 @@ function resetFlows(){
function bfs(){
let searches=[["s"]];
let visitedVertices=["s"];
let end=false;
while(searches.length>0){
currentSearch=searches.shift();
if(currentSearch[currentSearch.length-1]=="t"){
@ -229,6 +256,7 @@ function bfs(){
}
}
}
reachableFromStart=visitedVertices;
}
function selectShortestPath(){
result=bfs();
@ -243,7 +271,7 @@ function selectShortestPath(){
function selectRandomPath(){
flowVertices=["s"];
let visitedVertices=["s"];
for(let i=0;i<2000;i++){
for(let i=0;i<20000;i++){
calculatePossibleNextSteps();
let nextSteps=[];
for(let step of possibleNextSteps){
@ -350,12 +378,12 @@ function generateNewFlow(){
totalFlow+=newFlow;
flowVertices=["s"];
calculatePossibleNextSteps();
draw();
statusSim.innerText="Iteration "+iterations+". Augmentierender Pfad mit Flusskapazität "+newFlow+" hinzugefügt. Insgesamt hat der Fluss die Kapazität "+totalFlow+".";
if(bfs()==undefined){
statusSim.innerText+=" Es gibt keinen augmentierenden Pfad mehr, der Algorithmus ist fertig.";
done=true;
};
}
draw();
}
function resetToDefaultGraph(){
vDef.value="u;v;w;x;y;z;";