/
Graph.cs
1681 lines (1456 loc) · 42.3 KB
/
Graph.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace ShortestPaths
{
public interface IBaseVertex
{
int get_id();
double get_weight();
void set_weight(double weight);
}
public interface IBaseEdge
{
int get_weight();
Vertex get_start_vertex();
Vertex get_end_vertex();
}
public interface IBaseElementWithWeight
{
double get_weight();
}
public interface IBaseGraph
{
List<Vertex> get_vertex_list();
double get_edge_weight(Vertex source, Vertex sink);
HashSet<Vertex> get_adjacent_vertices(Vertex vertex);
HashSet<Vertex> get_precedent_vertices(Vertex vertex);
}
public class Vertex : IBaseVertex, IComparable<Vertex>
{
public static int CURRENT_VERTEX_NUM = 0;
private int _id = CURRENT_VERTEX_NUM++;
private double _weight = 0;
/**
*
*/
public int get_id()
{
return _id;
}
public override string ToString()
{
return " " + _id;
}
public double get_weight()
{
return _weight;
}
public void set_weight(double status)
{
_weight = status;
}
public int CompareTo(Vertex r_vertex)
{
double diff = this._weight - r_vertex._weight;
if (diff > 0)
return 1;
else if (diff < 0)
return -1;
else
return 0;
}
}
public class Path : IBaseElementWithWeight
{
List<Vertex> _vertex_list;
double _weight = -1;
public Path()
{
_vertex_list = new List<Vertex>();
}
public Path(List<Vertex> _vertex_list, double _weight)
{
this._vertex_list = _vertex_list;
this._weight = _weight;
}
public double get_weight()
{
return _weight;
}
public void set_weight(double weight)
{
_weight = weight;
}
public List<Vertex> get_vertices()
{
return _vertex_list;
}
public override int GetHashCode()
{
return _vertex_list.GetHashCode();
}
public override bool Equals(Object right)
{
if (right is Path)
{
Path r_path = right as Path;
return _vertex_list.Equals(r_path._vertex_list);
}
return false;
}
public override string ToString()
{
//return _vertex_list.ToString() + ":" + _weight;
string s = "Path = ";
foreach (Vertex vertex in _vertex_list)
{
s += vertex.ToString();
}
return s + " : " + _weight;
}
}
public class Graph : IBaseGraph
{
public static double DISCONNECTED;
// index of fan-outs of one vertex
protected Dictionary<int, HashSet<Vertex>> _fanout_vertices_index;
// index for fan-ins of one vertex
protected Dictionary<int, HashSet<Vertex>> _fanin_vertices_index;
// index for edge weights in the graph
protected Dictionary<KeyValuePair<int, int>, double> _vertex_pair_weight_index;
// index for vertices in the graph
protected Dictionary<int, Vertex> _id_vertex_index;
// list of vertices in the graph
protected List<Vertex> _vertex_list;
// the number of vertices in the graph
public int _vertex_num;
// the number of arcs in the graph
public int _edge_num;
/**
* Constructor 1
* @param data_file_name
*/
public Graph(string data_file_name)
{
DISCONNECTED = double.MaxValue;
// index of fan-outs of one vertex
_fanout_vertices_index = new Dictionary<int, HashSet<Vertex>>();
// index for fan-ins of one vertex
_fanin_vertices_index = new Dictionary<int, HashSet<Vertex>>();
// index for edge weights in the graph
_vertex_pair_weight_index = new Dictionary<KeyValuePair<int, int>, double>();
// index for vertices in the graph
_id_vertex_index = new Dictionary<int, Vertex>();
// list of vertices in the graph
_vertex_list = new List<Vertex>();
// the number of vertices in the graph
_vertex_num = 0;
// the number of arcs in the graph
_edge_num = 0;
import_from_file(data_file_name);
}
/**
* Constructor 2
*
* @param graph
*/
public Graph(Graph graph_)
{
DISCONNECTED = double.MaxValue;
// index of fan-outs of one vertex
_fanout_vertices_index = new Dictionary<int, HashSet<Vertex>>(graph_._fanout_vertices_index);
// index for fan-ins of one vertex
_fanin_vertices_index = new Dictionary<int, HashSet<Vertex>>(graph_._fanin_vertices_index);
// index for edge weights in the graph
_vertex_pair_weight_index = new Dictionary<KeyValuePair<int, int>, double>(graph_._vertex_pair_weight_index);
// index for vertices in the graph
_id_vertex_index = new Dictionary<int, Vertex>(graph_._id_vertex_index);
// list of vertices in the graph
_vertex_list = new List<Vertex>(graph_._vertex_list);
// the number of vertices in the graph
_vertex_num = graph_._vertex_num;
// the number of arcs in the graph
_edge_num = graph_._edge_num;
}
/**
* Default constructor
*/
public Graph()
{
DISCONNECTED = double.MaxValue;
// index of fan-outs of one vertex
_fanout_vertices_index = new Dictionary<int, HashSet<Vertex>>();
// index for fan-ins of one vertex
_fanin_vertices_index = new Dictionary<int, HashSet<Vertex>>();
// index for edge weights in the graph
_vertex_pair_weight_index = new Dictionary<KeyValuePair<int, int>, double>();
// index for vertices in the graph
_id_vertex_index = new Dictionary<int, Vertex>();
// list of vertices in the graph
_vertex_list = new List<Vertex>();
// the number of vertices in the graph
_vertex_num = 0;
// the number of arcs in the graph
_edge_num = 0;
}
/**
* Clear members of the graph.
*/
public void Clear()
{
_vertex_num = 0;
_edge_num = 0;
_vertex_list.Clear();
_id_vertex_index.Clear();
_fanin_vertices_index.Clear();
_fanout_vertices_index.Clear();
_vertex_pair_weight_index.Clear();
}
/**
* There is a requirement for the input graph.
* The ids of vertices must be consecutive.
*
* @param data_file_name
*/
public void import_from_file(string data_file_name)
{
try
{
// 1. read the file and put the content in the buffer
TextReader bufRead = new StreamReader(data_file_name);
bool is_first_line = true;
string line; // String that holds current file line
// 2. Read first line
line = bufRead.ReadLine();
while (line != null)
{
// 2.1 skip the empty line
if (line.Trim().Equals(""))
{
line = bufRead.ReadLine();
continue;
}
// 2.2 generate nodes and edges for the graph
if (is_first_line)
{
//2.2.1 obtain the number of nodes in the graph
Vertex.CURRENT_VERTEX_NUM = 0;
is_first_line = false;
_vertex_num = int.Parse(line.Trim()) + 1;
for (int i = 0; i < _vertex_num; ++i)
{
Vertex vertex = new Vertex();
_vertex_list.Add(vertex);
_id_vertex_index[vertex.get_id()] = vertex;
}
}
else
{
//2.2.2 find a new edge and put it in the graph
string[] str_list = line.Trim().Split('\r', '\n', '\t', ' ');
int start_vertex_id = int.Parse(str_list[0]);
int end_vertex_id = int.Parse(str_list[1]);
double weight = double.Parse(str_list[2]);
add_edge(start_vertex_id, end_vertex_id, weight);
}
//
line = bufRead.ReadLine();
}
bufRead.Close();
}
catch (IOException e)
{
// If another exception is generated, print a stack trace
Console.Write(e.Message);
}
}
/**
* Note that this may not be used externally, because some other members in the class
* should be updated at the same time.
*
* @param start_vertex_id
* @param end_vertex_id
* @param weight
*/
protected void add_edge(int start_vertex_id, int end_vertex_id, double weight)
{
// actually, we should make sure all vertices ids must be correct.
if (!_id_vertex_index.ContainsKey(start_vertex_id)
|| !_id_vertex_index.ContainsKey(end_vertex_id)
|| start_vertex_id == end_vertex_id)
{
throw new System.ArgumentOutOfRangeException("The edge from " + start_vertex_id
+ " to " + end_vertex_id + " does not exist in the graph.");
}
// update the adjacent-list of the graph
HashSet<Vertex> fanout_vertex_set;
if (_fanout_vertices_index.ContainsKey(start_vertex_id))
{
fanout_vertex_set = _fanout_vertices_index[start_vertex_id];
}
else
{
fanout_vertex_set = new HashSet<Vertex>();
_fanout_vertices_index.Add(start_vertex_id, fanout_vertex_set);
}
fanout_vertex_set.Add(_id_vertex_index[end_vertex_id]);
//
HashSet<Vertex> fanin_vertex_set;
if (_fanin_vertices_index.ContainsKey(end_vertex_id))
{
fanin_vertex_set = _fanin_vertices_index[end_vertex_id];
}
else
{
fanin_vertex_set = new HashSet<Vertex>();
_fanin_vertices_index.Add(end_vertex_id, fanin_vertex_set);
}
fanin_vertex_set.Add(_id_vertex_index[start_vertex_id]);
// store the new edge
_vertex_pair_weight_index.Add(new KeyValuePair<int, int>(start_vertex_id, end_vertex_id),
weight);
++_edge_num;
}
/**
* Store the graph information into a file.
*
* @param file_name
*/
public void export_to_file(string file_name)
{
try
{
//1. prepare the text to export
TextWriter tw = new StreamWriter(file_name);
tw.WriteLine(_vertex_num);
foreach (KeyValuePair<KeyValuePair<int, int>, double> cur_edge_pair in _vertex_pair_weight_index)
{
KeyValuePair<int, int> cur_edge = cur_edge_pair.Key;
int starting_pt_id = cur_edge.Key;
int ending_pt_id = cur_edge.Value;
double weight = cur_edge_pair.Value;
tw.WriteLine(starting_pt_id + "\t" + ending_pt_id + "\t" + weight);
}
tw.Close();
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
}
/* (non-Javadoc)
* @see edu.asu.emit.qyan.alg.model.abstracts.BaseGraph#get_adjacent_vertices(edu.asu.emit.qyan.alg.model.abstracts.BaseVertex)
*/
public virtual HashSet<Vertex> get_adjacent_vertices(Vertex vertex)
{
return _fanout_vertices_index.ContainsKey(vertex.get_id())
? _fanout_vertices_index[vertex.get_id()]
: new HashSet<Vertex>();
}
/* (non-Javadoc)
* @see edu.asu.emit.qyan.alg.model.abstracts.BaseGraph#get_precedent_vertices(edu.asu.emit.qyan.alg.model.abstracts.BaseVertex)
*/
public virtual HashSet<Vertex> get_precedent_vertices(Vertex vertex)
{
return _fanin_vertices_index.ContainsKey(vertex.get_id())
? _fanin_vertices_index[vertex.get_id()]
: new HashSet<Vertex>();
}
/* (non-Javadoc)
* @see edu.asu.emit.qyan.alg.model.abstracts.BaseGraph#get_edge_weight(edu.asu.emit.qyan.alg.model.abstracts.BaseVertex, edu.asu.emit.qyan.alg.model.abstracts.BaseVertex)
*/
public virtual double get_edge_weight(Vertex source, Vertex sink)
{
KeyValuePair<int, int> Key = new KeyValuePair<int, int>(source.get_id(), sink.get_id());
return (_vertex_pair_weight_index.ContainsKey(Key)) ?
_vertex_pair_weight_index[Key] : DISCONNECTED;
}
/**
* Set the number of vertices in the graph
* @param num
*/
public virtual void set_vertex_num(int num)
{
_vertex_num = num;
}
/**
* Return the vertex list in the graph.
*/
public virtual List<Vertex> get_vertex_list()
{
return _vertex_list;
}
/**
* Get the vertex with the input id.
*
* @param id
* @return
*/
public virtual Vertex get_vertex(int id)
{
return _id_vertex_index[id];
}
}
public class VariableGraph : Graph
{
HashSet<int> _rem_vertex_id_set;
HashSet<KeyValuePair<int, int>> _rem_edge_set;
/**
* Default constructor
*/
public VariableGraph()
{
_rem_vertex_id_set = new HashSet<int>();
_rem_edge_set = new HashSet<KeyValuePair<int, int>>();
}
/**
* Constructor 1
*
* @param data_file_name
*/
public VariableGraph(string data_file_name)
: base(data_file_name)
{
_rem_vertex_id_set = new HashSet<int>();
_rem_edge_set = new HashSet<KeyValuePair<int, int>>();
}
/**
* Constructor 2
*
* @param graph
*/
public VariableGraph(Graph graph)
: base(graph)
{
_rem_vertex_id_set = new HashSet<int>();
_rem_edge_set = new HashSet<KeyValuePair<int, int>>();
}
/**
* Set the set of vertices to be removed from the graph
*
* @param _rem_vertex_list
*/
public void set_rem_vertex_id_list(ICollection<int> _rem_vertex_list)
{
this._rem_vertex_id_set.UnionWith(_rem_vertex_list);
}
/**
* Set the set of edges to be removed from the graph
*
* @param _rem_edge_hashcode_set
*/
public void set_rem_edge_hashcode_set(ICollection<KeyValuePair<int, int>> rem_edge_collection)
{
_rem_edge_set.UnionWith(rem_edge_collection);
}
/**
* Add an edge to the set of removed edges
*
* @param edge
*/
public void remove_edge(KeyValuePair<int, int> edge)
{
_rem_edge_set.Add(edge);
}
/**
* Add a vertex to the set of removed vertices
*
* @param vertex_id
*/
public void remove_vertex(int vertex_id)
{
_rem_vertex_id_set.Add(vertex_id);
}
public void recover_removed_edges()
{
_rem_edge_set.Clear();
}
public void recover_removed_edge(KeyValuePair<int, int> edge)
{
_rem_edge_set.Remove(edge);
}
public void recover_removed_vertices()
{
_rem_vertex_id_set.Clear();
}
public void recover_removed_vertex(int vertex_id)
{
_rem_vertex_id_set.Remove(vertex_id);
}
/**
* Return the weight associated with the input edge.
*
* @param source
* @param sink
* @return
*/
public override double get_edge_weight(Vertex source, Vertex sink)
{
int source_id = source.get_id();
int sink_id = sink.get_id();
if (_rem_vertex_id_set.Contains(source_id) || _rem_vertex_id_set.Contains(sink_id)
|| _rem_edge_set.Contains(new KeyValuePair<int, int>(source_id, sink_id)))
{
return Graph.DISCONNECTED;
}
return base.get_edge_weight(source, sink);
}
/**
* Return the weight associated with the input edge.
*
* @param source
* @param sink
* @return
*/
public double get_edge_weight_of_graph(Vertex source, Vertex sink)
{
return base.get_edge_weight(source, sink);
}
/**
* Return the set of fan-outs of the input vertex.
*
* @param vertex
* @return
*/
public override HashSet<Vertex> get_adjacent_vertices(Vertex vertex)
{
HashSet<Vertex> ret_set = new HashSet<Vertex>();
int starting_vertex_id = vertex.get_id();
if (!_rem_vertex_id_set.Contains(starting_vertex_id))
{
HashSet<Vertex> adj_vertex_set = base.get_adjacent_vertices(vertex);
foreach (Vertex cur_vertex in adj_vertex_set)
{
int ending_vertex_id = cur_vertex.get_id();
if (_rem_vertex_id_set.Contains(ending_vertex_id)
|| _rem_edge_set.Contains(
new KeyValuePair<int, int>(starting_vertex_id, ending_vertex_id)))
{
continue;
}
//
ret_set.Add(cur_vertex);
}
}
return ret_set;
}
/**
* Get the set of vertices preceding the input vertex.
*
* @param vertex
* @return
*/
public override HashSet<Vertex> get_precedent_vertices(Vertex vertex)
{
HashSet<Vertex> ret_set = new HashSet<Vertex>();
if (!_rem_vertex_id_set.Contains(vertex.get_id()))
{
int ending_vertex_id = vertex.get_id();
HashSet<Vertex> pre_vertex_set = base.get_precedent_vertices(vertex);
foreach (Vertex cur_vertex in pre_vertex_set)
{
int starting_vertex_id = cur_vertex.get_id();
if (_rem_vertex_id_set.Contains(starting_vertex_id)
|| _rem_edge_set.Contains(
new KeyValuePair<int, int>(starting_vertex_id, ending_vertex_id)))
{
continue;
}
//
ret_set.Add(cur_vertex);
}
}
return ret_set;
}
/**
* Get the list of vertices in the graph, except those removed.
* @return
*/
public override List<Vertex> get_vertex_list()
{
List<Vertex> ret_list = new List<Vertex>();
foreach (Vertex cur_vertex in base.get_vertex_list())
{
if (_rem_vertex_id_set.Contains(cur_vertex.get_id())) continue;
ret_list.Add(cur_vertex);
}
return ret_list;
}
/**
* Get the vertex corresponding to the input 'id', if exist.
*
* @param id
* @return
*/
public override Vertex get_vertex(int id)
{
if (_rem_vertex_id_set.Contains(id))
{
return null;
}
else
{
return base.get_vertex(id);
}
}
}
public class QYPriorityQueue<T> where T : IBaseElementWithWeight
{
List<T> _element_weight_pair_list;
int _limit_size = -1;
bool _is_incremental = false;
/**
* Default constructor.
*/
public QYPriorityQueue()
{
_element_weight_pair_list = new List<T>();
}
/**
* Constructor.
* @param limit_size
*/
public QYPriorityQueue(int limit_size, bool is_incremental)
{
_limit_size = limit_size;
_is_incremental = is_incremental;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public override string ToString()
{
return _element_weight_pair_list.ToString();
}
/**
* Binary search is exploited to find the right position
* of the new element.
* @param weight
* @return the position of the new element
*/
private int _bin_locate_pos(double weight, bool is_incremental)
{
int mid = 0;
int low = 0;
int high = _element_weight_pair_list.Count - 1;
//
while (low <= high)
{
mid = (low + high) / 2;
if (_element_weight_pair_list.ElementAt(mid).get_weight() == weight)
return mid + 1;
if (is_incremental)
{
if (_element_weight_pair_list.ElementAt(mid).get_weight() < weight)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
else
{
if (_element_weight_pair_list.ElementAt(mid).get_weight() > weight)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
}
return low;
}
/**
* Add a new element in the queue.
* @param element
*/
public void add(T element)
{
_element_weight_pair_list.Insert(_bin_locate_pos(element.get_weight(), _is_incremental), element);
if (_limit_size > 0 && _element_weight_pair_list.Count > _limit_size)
{
int size_of_results = _element_weight_pair_list.Count;
_element_weight_pair_list.RemoveAt(size_of_results - 1);
}
}
/**
* It only reflects the size of the current results.
* @return
*/
public int size()
{
return _element_weight_pair_list.Count;
}
/**
* Get the i th element.
* @param i
* @return
*/
public T get(int i)
{
if (i >= _element_weight_pair_list.Count)
{
Console.WriteLine("The result :" + i + " doesn't exist!!!");
}
return _element_weight_pair_list[i];
}
/**
* Get the first element, and then remove it from the queue.
* @return
*/
public T poll()
{
T ret = _element_weight_pair_list[0];
_element_weight_pair_list.RemoveAt(0);
return ret;
}
/**
* Check if it's empty.
* @return
*/
public bool isEmpty()
{
return (_element_weight_pair_list.Count == 0);
}
public void Clear()
{
_element_weight_pair_list.Clear();
}
}
public class VPriorityQueue<T> where T : IBaseVertex
{
List<T> _element_weight_pair_list;
int _limit_size = -1;
bool _is_incremental = false;
/**
* Default constructor.
*/
public VPriorityQueue()
{
_element_weight_pair_list = new List<T>();
}
/**
* Constructor.
* @param limit_size
*/
public VPriorityQueue(int limit_size, bool is_incremental)
{
_limit_size = limit_size;
_is_incremental = is_incremental;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public override string ToString()
{
return _element_weight_pair_list.ToString();
}
/**
* Binary search is exploited to find the right position
* of the new element.
* @param weight
* @return the position of the new element
*/
private int _bin_locate_pos(double weight, bool is_incremental)
{
int mid = 0;
int low = 0;
int high = _element_weight_pair_list.Count - 1;
//
while (low <= high)
{
mid = (low + high) / 2;
if (_element_weight_pair_list.ElementAt(mid).get_weight() == weight)
return mid + 1;
if (is_incremental)
{
if (_element_weight_pair_list.ElementAt(mid).get_weight() < weight)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
else
{
if (_element_weight_pair_list.ElementAt(mid).get_weight() > weight)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
}
return low;
}
/**
* Add a new element in the queue.
* @param element
*/
public void Add(T element)
{
_element_weight_pair_list.Insert(_bin_locate_pos(element.get_weight(), _is_incremental), element);
if (_limit_size > 0 && _element_weight_pair_list.Count > _limit_size)
{
int size_of_results = _element_weight_pair_list.Count;
_element_weight_pair_list.RemoveAt(size_of_results - 1);
}
}
/**
* It only reflects the size of the current results.
* @return
*/
public int Size()
{
return _element_weight_pair_list.Count;
}
/**
* Get the i th element.
* @param i
* @return
*/
public T Get(int i)
{
if (i >= _element_weight_pair_list.Count)
{
Console.WriteLine("The result :" + i + " doesn't exist!!!");
}
return _element_weight_pair_list[i];
}
/**
* Get the first element, and then remove it from the queue.
* @return
*/
public T Poll()
{
T ret = _element_weight_pair_list[0];
_element_weight_pair_list.RemoveAt(0);
return ret;
}
/**
* Check if it's empty.
* @return
*/
public bool IsEmpty()
{
return (_element_weight_pair_list.Count == 0);
}
public void Clear()
{
_element_weight_pair_list.Clear();
}
}
public class DijkstraShortestPathAlg
{
// Input
Graph _graph = null;
// Intermediate variables
HashSet<Vertex> _determined_vertex_set;
VPriorityQueue<Vertex> _vertex_candidate_queue;
Dictionary<Vertex, Double> _start_vertex_distance_index;
Dictionary<Vertex, Vertex> _predecessor_index;
/**
* Default constructor.
* @param graph
*/
public DijkstraShortestPathAlg(Graph graph)
{
_determined_vertex_set = new HashSet<Vertex>();
_vertex_candidate_queue = new VPriorityQueue<Vertex>();
_start_vertex_distance_index = new Dictionary<Vertex, double>();
_predecessor_index = new Dictionary<Vertex, Vertex>();
_graph = graph;
}