#C18.txt: March 31, 2025 Graph algithms Help18:=proc(): print(` RWG(n,p,K), Dij(G,a) , DijP(G,a) `): end: #RWG(n,p,K): inputs a pos. integer n and outputs a triple [n,E,DT] #where the vertices are {1,...,n} the edges are of the form {i,j} and #DT is table such DT[{i,j}] is the weight of the edge {i,j} RWG:=proc(n,p,K) local G,DT,e,ra,E: ra:=rand(1..K): G:=RG(n,p): E:=G[2]: for e in E do DT[e]:=ra(): od: [n,E,op(DT)]: end: #Dij(G,a): inputs a weighted graph G=[n,E,DT] and outputs and a starting #vertex a outputs a list L of length n such that L[i] is the MINIMAL DISTANCE #min. distance #from a to i. In particular L[a]=0 Dij:=proc(G,a) local n,E,DT,Nei,e,i,Uvis,Tt,T,Vis,cu,Nei1,v,cha,rec,i1: n:=G[1]: E:=G[2]: DT:=G[3]: #Nei is a table of length n such that N[i] is the set of VERTICES j such that {i,j} belongs to E for i from 1 to n do Nei[i]:={}: od: for e in E do Nei[e[1]]:=Nei[e[1]] union {e[2]}: Nei[e[2]]:=Nei[e[2]] union {e[1]}: od: #Uvis is he currently set of still unvisoted vertices Uvis:={seq(i,i=1..n)}: #T[i] is the permanent label (the min. distance from a to i #Tt[i]: is the current best upper bound for i from 1 to n do Tt[i]:=infinity: od: T[a]:=0: Uvis:=Uvis minus {a}: Vis:=[a]: while Uvis<>{} do cu:=Vis[nops(Vis)]: Nei1:=Nei[cu]: for v in Nei1 do if T[cu]+DT[{cu,v}]{} do cu:=Vis[nops(Vis)]: Nei1:=Nei[cu]: for v in Nei1 do if T[cu]+DT[{cu,v}]=0 and p<=1) then RETURN(FAIL): fi: a:=numer(p): b:=denom(p): ra:=rand(1..b)(): if ra<=a then true: else false: fi: end: RG:=proc(n,p) local E,i,j: E:={}: for i from 1 to n do for j from i+1 to n do if LC(p) then E:=E union {{i,j}}: fi: od: od: [n,E]: end: ####end from C2.txt