//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.24; contract Sorter { uint public totalUsers = 0; // userID -> index mapping (uint => uint) public userMap; string[][2] public userDetails; int[][][2] public adjMatrix; // constructor(uint initVal) public { // // loopVar = initVal*50; // totalUsers = 0; // } // function runLoop() view public{ // uint a=0; // for (uint i = 0; i < loopVar; i++) // { // a++; // } // } function registerUser(uint userID, string userName) view public{ // userDetails[totalUsers][0] = string(userID); // userDetails[totalUsers][0] = userName; userMap[userID] = totalUsers; for (uint i = 0; i < totalUsers; i++){ adjMatrix[totalUsers][i][0] = -1; adjMatrix[totalUsers][i][1] = -1; adjMatrix[i][totalUsers][0] = -1; adjMatrix[i][totalUsers][1] = -1; } totalUsers += 1; } function createAcc(uint userID1, uint userID2, uint sampledValue) view public{ // sampledValue will come from python script. Solidity doesn't have exponential dist. // The sampled value must be an even integer (solidity doesn't support float/double either) adjMatrix[userMap[userID1]][userMap[userID2]][0] = sampledValue/2; adjMatrix[userMap[userID1]][userMap[userID2]][1] = sampledValue/2; adjMatrix[userMap[userID2]][userMap[userID1]][0] = sampledValue/2; adjMatrix[userMap[userID2]][userMap[userID1]][1] = sampledValue/2; } function shortestPath(uint userID1, uint userID2) public view returns(uint[] memory){ // return userID } function sendAmount(uint userID1, uint userID2) view public{ // find the shortest path between userMap[userID1], userMap[userID2] // path = [a, b, c, d]; a->d uint[totalUsers] visited = 0; uint[] queue = []; uint[totalUsers] dist = INT_MAX; uint[totalUsers] parent = -1; uint src = userMap(userID1); uint dest = userMap(userID2); queue.push(src); visited[src] = 1; dist[src] = 0; uint flag = 0; while len(queue)!=0: uint node = queue.pop(); for i in range(totalUsers): if adjMatrix[node][i] != [-1, -1] and adjMatrix[node][i][0] >= 1: if visited[i] == 0: visited[i] = 1; dist[i] = dist[node] + 1; parent[i] = node; queue.append(i); if (i == dest): flag = 1 break if (flag == 0): print("no path found"); else: uint size = 0; uint[] path; node = dest; while node != src: path.append(node); size += 1; node = parent[node]; path.append(src); size += 1 return path; uint[] path = shortestPath(userID1, userID2); for (uint i = 0; i < path.length - 1; i++){ adjMatrix[uint(userMap[path[i]])][uint(userMap[path[i+1]])][0] -= 1; adjMatrix[uint(path[i])][uint(path[i+1])][1] += 1; } } function closeAcc(uint userID1, uint userID2) view public{ adjMatrix[userMap[userID1]][userMap[userID2]][0] = -1; adjMatrix[userMap[userID1]][userMap[userID2]][1] = -1; } }
0.4.18