Example #1
0
        private void addEdge(int src, int dst, int weight)
        {
            Tbdistance a = new Tbdistance(), b = new Tbdistance();

            a.distance   = b.distance = weight;
            a.station1Id = b.station2id = src;
            a.station2id = b.station1Id = dst;
            routePool    = routePool.Concat(new Tbdistance[] { a, b }).ToArray();
        }
Example #2
0
        private void primAlgo()
        {
            output.Text = "";//the output to be returned and the end of the function

            /*since it is diificult to push a element dynamicaly to a fixed array the variable selectedStationList
             * maintains a list of user selected stations as string comma seperated and after collections split the string
             * as an array to access each element by given index*/
            String[] stationsIdsToBeConnected = selectedStationList.Split(',');


            String[] visitedStationIds = new String[stationsIdsToBeConnected.Length];

            //the first station is visited intitially, variable visitedStationIds maintains arrays of visited
            //stations
            visitedStationIds[0] = stationsIdsToBeConnected[0];
            Tbdistance[] stageData   = null;
            int          count       = 1;//since one station is visited initially the count starts from 1
            int          totalWeight = 0;

            while (count < stationsIdsToBeConnected.Length)//iterate until all the stations are connected/visited..
            {
                /*from list of all the interconnect (include both directional edges) routes
                 * connected to user given station, find set of routes where each edge's starting station is already visited
                 * but destination station is unvisited and a station which user has already defined*/
                stageData = possibleRoutes.Where(
                    //check if any of ids of visited station match/not matching
                    r => visitedStationIds.Contains(r.station1Id.ToString()) &&
                    !visitedStationIds.Contains(r.station2id.ToString()) &&
                    stationsIdsToBeConnected.Contains(r.station1Id.ToString())).ToArray();

                Tbdistance EdgeWithMinWeight = null;
                int        minWeight         = int.MaxValue;//minWeight is set to int defined possible infinite
                /*From resultant set edges find an edge with minimum distance/weight*/
                foreach (Tbdistance d in stageData)
                {
                    if (d.distance < minWeight)
                    {
                        EdgeWithMinWeight = d;
                        minWeight         = d.distance.Value;
                    }
                }
                //mark the destination/connected station of edge with min weight to be visited
                visitedStationIds[count] = EdgeWithMinWeight.station2id.ToString();
                count++;
                //increment the tota; weight of minimum spanning tree with discovered min weight
                totalWeight += minWeight;
                //In each iterartion display the edge with min weight...
                output.Text += findStationName(EdgeWithMinWeight.station1Id.Value) + "<-->" + findStationName(EdgeWithMinWeight.station2id.Value) + " " + EdgeWithMinWeight.distance + Environment.NewLine;
            }
            output.Text += "Total weight " + totalWeight;
        }