Post Reply 
minimum spanning tree lua HPPL
10-28-2021, 01:04 PM
Post: #1
minimum spanning tree lua HPPL
Good morning Albert Chan, here is the program, which seems to work, but it gives wrong results. For instance:
Points: = {{3,4.5},{1,5},{5,6.5},{2.5,5},{7,3},{1.5,1},{4,2.5},{2,2},{8,4},{3,9},{4.5,0.5}​,{5,2}};
Edges: = {{3,4.5,1,5,3,4.5,5,6.5,3,4.5,2.5,5,3,4.5,7,3,3,4.5,1.5,1,3,4.5,4,2.5,3,4.5,2,2,​3,4.5,8,4,3,4.5,3,9,3,4.5,4.5,0.5,3,4.5,5,2},{1,5,5,6.5,1,5,2.5,5,1,5,7,3,1,5,1.​5,1,1,5,4,2.5,1,5,2,2,1,5,8,4,1,5,3,9,1,5,4.5,0.5,1,5,5,2},{5,6.5,2.5,5,5,6.5,7,​3,5,6.5,1.5,1,5,6.5,4,2.5,5,6.5,2,2,5,6.5,8,4,5,6.5,3,9,5,6.5,4.5,0.5,5,6.5,5,2}​,{2.5,5,7,3,2.5,5,1.5,1,2.5,5,4,2.5,2.5,5,2,2,2.5,5,8,4,2.5,5,3,9,2.5,5,4.5,0.5,​2.5,5,5,2},{7,3,1.5,1,7,3,4,2.5,7,3,2,2,7,3,8,4,7,3,3,9,7,3,4.5,0.5,7,3,5,2},{1.​5,1,4,2.5,1.5,1,2,2,1.5,1,8,4,1.5,1,3,9,1.5,1,4.5,0.5,1.5,1,5,2},{4,2.5,2,2,4,2.​5,8,4,4,2.5,3,9,4,2.5,4.5,0.5,4,2.5,5,2},{2,2,8,4,2,2,3,9,2,2,4.5,0.5,2,2,5,2},{​8,4,3,9,8,4,4.5,0.5,8,4,5,2},{3,9,4.5,0.5,3,9,5,2},{4.5,0.5,5,2}};


To compare the wrong result to the right result, I would like to send you an image, but I cannot find the "attach image" option. So I'm sending you a message on the forum.
Thanks for your availability, greetings, Roberto.

Code:

#cas
dist(x1,y1,x2,y2):=
BEGIN
RETURN sqrt((ABS(x2-x1))^2+(ABS(y2-y1))^2);
END;

same(x1,y1,x2,y2,edg):=
BEGIN
FOR jj FROM 1 TO length(edg) DO
IF (x1==edg(jj)(1) AND y1==edg(jj)(2) AND x2==edg(jj)(3) AND y2==edg(jj)(4)) OR (x2==edg(jj)(1) AND y2==edg(jj)(2) AND x1==edg(jj)(3) AND y1==edg(jj)(4))
THEN
RETURN true;
END;
END;
RETURN false;
END;

doesexist(lst,item):=
BEGIN
FOR uu FROM 1 TO length(lst) DO
IF lst(uu)==item THEN
RETURN true;
END;
END;
RETURN false;
END;

mstRob(points,edges):=
BEGIN
verticesi:={points(1)};
tree:={};
WHILE length(verticesi) != length(points) DO
  lnn:=10000;
  temp1:={};
  temp2:={};
  FOR j FROM 1 TO length(verticesi) DO
    FOR pp FROM 1 TO length(points) DO
      IF doesexist(verticesi,points(pp))==false THEN
        IF dist(points(pp)(1),points(pp)(2),verticesi(j)(1),verticesi(j)(2))<lnn AND same(points(pp)(1),points(pp)(2),verticesi(j)(1),verticesi(j)(2),edges) THEN
          lnn:=dist(points(pp)(1),points(pp)(2),verticesi(j)(1),verticesi(j)(2));
          temp1:=points(pp);
          temp2:=verticesi(j);
        END;
      END;
    END;
  END;
  verticesi:=append(verticesi,temp1);
  tree:=append(tree,{temp1(1),temp1(2),temp2(1),temp2(2)});
END;
RETURN tree;
END;
#end


Attached File(s) Thumbnail(s)
   
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
minimum spanning tree lua HPPL - robmio - 10-28-2021 01:04 PM



User(s) browsing this thread: 2 Guest(s)