LinkedList <Tbdistances> addDes = new LinkedList <Tbdistances>(); // this is insert link list private void Add_Click(object sender, EventArgs e) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Tbdistances a = new Tbdistances(), b = new Tbdistances(); // create two tbsistance table objects a.station1id = Convert.ToInt32(loc1.SelectedValue); // get data from station1 cobobox and convert to integer a.station2id = Convert.ToInt32(Loc2.SelectedValue); // get data from station2 cobobox and convert to integer b.station1id = a.station2id; //this logic use to ex if we store A->B distance 10, B-> A should be 10 b.station2id = a.station1id; if (Text_distance.Text.Length == 0) { MessageBox.Show("Please enter distance"); return; } a.distance = Convert.ToInt32(Text_distance.Text); b.distance = a.distance; addDes.AddLast(a); // add data to link list addDes.AddLast(b); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); lbl_time.Text = "single Add distance time " + elapsedTime; Text_distance.Text = ""; }
LinkedList <Tbdistances> deleteDes = new LinkedList <Tbdistances>(); // this using to deleted values store private void btn_distance_delete_Click(object sender, EventArgs e) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Tbdistances add = new Tbdistances(), add2 = new Tbdistances(); //create two objects if (txt_distance_search.Text.Length == 0) { MessageBox.Show("Please enter an ID"); return; } add.id = Convert.ToInt32(txt_distance_search.Text); //if we delete the A->B =15 from id add2.id = findAjacentIDEdge(add.id); // we should delete the B->A =15 from id this methed help to find that id deleteDes.AddLast(add); //A->B id add the link list deleteDes.AddLast(add2); //B->A id add the link list stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); lbl_time.Text = "single delete distance time " + elapsedTime; Text_distance.Text = ""; txt_distance_search.Text = ""; }
private int findAjacentIDEdge(int id) { //find the station A with id=id in the argument Tbdistances A = db.stationDistances.Find(id); //get station1 id and station2 id from the found edge... //find the edge with station ids matching vice versa...and return it's id return(db.stationDistances.Single(d => d.station1id == A.station2id && d.station2id == A.station1id).id); }
public String primAlgo() { if (selectedStations == null || selectedStations.Count == 0) { return("Station not selected"); } String output = ""; double totalWeight = 0; LinkedList <int> selectedStationIDs = new LinkedList <int>(); foreach (tbStations s in selectedStations) { selectedStationIDs.AddFirst(s.id); } LinkedList <tbStations> markedtbStations = new LinkedList <tbStations>(); LinkedList <Tbdistances> comparingEdges = new LinkedList <Tbdistances>(); markedtbStations.AddFirst(selectedStations.First.Value); while (markedtbStations.Count < selectedStations.Count) { foreach (tbStations s in markedtbStations) { foreach (Tbdistances d in s.StationDistances1) { if (selectedStationIDs.Contains(d.Stations2.id) && !markedtbStations.Contains(d.Stations2)) { comparingEdges.AddFirst(d); } } } double minWeight = int.MaxValue; Tbdistances minWeightEdge = null; foreach (Tbdistances d in comparingEdges) { if (d.distance < minWeight) { minWeight = d.distance; minWeightEdge = d; } } comparingEdges.Clear(); output += minWeight.ToString() + " " + minWeightEdge.Stations1.Name + "->" + minWeightEdge.Stations2.Name + Environment.NewLine; totalWeight += minWeight; markedtbStations.AddFirst(minWeightEdge.Stations2); } output += "Total weight " + totalWeight; return(output); }
private void Add_All_Click(object sender, EventArgs e) { while (addDes.Count > 0) { AddDistance a = new AddDistance(); a = addDes.Dequeue(); Tbdistances stationDistance = new Tbdistances(); stationDistance.station1id = a.stationone; stationDistance.station2id = a.stationtwo; stationDistance.distance = a.distance; db.stationDistances.Add(stationDistance); db.SaveChanges(); } }
private void btn_distance_deleteall_Click(object sender, EventArgs e) // delete all values { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); foreach (Tbdistances deletedistance in deleteDes) //read all datas { Tbdistances removestations = db.stationDistances.Find(deletedistance.id); db.stationDistances.Remove(removestations); db.SaveChanges(); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); lbl_time.Text = "Multiple delete distance time " + elapsedTime; list = db.stations.ToList(); listdistance = db.stationDistances.ToList(); loadComboData(); }
LinkedList <Tbdistances> updateDes = new LinkedList <Tbdistances>(); //create the link list for update distance values store private void btn_update_distance_Click(object sender, EventArgs e) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Tbdistances add = new Tbdistances(), add2 = new Tbdistances();; //create two objects to update values store if (txt_distance_search.Text.Length == 0) { MessageBox.Show("Please enter an ID"); return; } if (Text_distance.Text.Length == 0) { MessageBox.Show("Please enter distance"); return; } add.id = Convert.ToInt32(txt_distance_search.Text); // add object to search textfield value add.distance = Convert.ToInt32(Text_distance.Text); //distance add add.station1id = Convert.ToInt32(loc1.SelectedValue); //add station 1 id add.station2id = Convert.ToInt32(Loc2.SelectedValue); //add station 2 id add2.id = findAjacentIDEdge(add.id); // if we update A->B=15 then we should update B->A=15 this methed help to find add2.distance = add.distance; add2.station1id = add.station2id; add2.station2id = add.station1id; updateDes.AddLast(add); // add updated data to link list (ex: A->B=15) updateDes.AddLast(add2); // add updated data to link list (ex: B->A=15) stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); lbl_time.Text = "single update distance time " + elapsedTime; Text_distance.Text = ""; txt_distance_search.Text = ""; }
private void btn_update_distanceall_Click(object sender, EventArgs e) // update all distance { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); foreach (Tbdistances adddistance in updateDes) //read all updated data { Tbdistances add = new Tbdistances(); add = db.stationDistances.Find(adddistance.id); add.station1id = adddistance.station1id; add.station2id = adddistance.station2id; add.distance = adddistance.distance; db.Entry(add).State = EntityState.Modified; db.SaveChanges(); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); lbl_time.Text = "Multiple distance update time " + elapsedTime; list = db.stations.ToList(); listdistance = db.stationDistances.ToList(); loadComboData(); }
private void Add_All_Click(object sender, EventArgs e) //add all the distance { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); foreach (Tbdistances adddistance in addDes) { Tbdistances stationDistance = new Tbdistances(); stationDistance.station1id = adddistance.station1id; //add data station1id stationDistance.station2id = adddistance.station2id; //add data station2id stationDistance.distance = adddistance.distance; // add distance db.stationDistances.Add(stationDistance); db.SaveChanges(); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); lbl_time.Text = "Multiple Add Distance time " + elapsedTime; list = db.stations.ToList(); listdistance = db.stationDistances.ToList(); loadComboData(); }