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
//! A [graph](http://en.wikipedia.org/wiki/Graph_(abstract_data_type))
//! representation and search library
//!
//! # Example
//! ```
//! use graphsearch::{Graph, Node, Vertex};
//! let rawgraph = vec![Node{content: "Helsinki",
//!                          adjacent: vec![Vertex{cost: 20, node: 1},
//!                                         Vertex{cost: 50, node: 2}]},
//!                     Node{content: "Turku",
//!                          adjacent: vec![Vertex{cost: 30, node: 2}]},
//!                     Node{content: "Tampere",
//!                          adjacent: Vec::new()}];
//! let g = Graph::new(rawgraph);
//! let start  = 0;
//! let target = "Tampere";
//! let res = g.search(start, target); // uses dijkstras algorithm
//! match res {
//!   None => {
//!     println!("Search returned None");
//!   }
//!   Some(result) => {
//!     println!("Search returned a path: {:?}", result);
//!     println!("The returned path cost: {}", g.cost_of_path(&result));
//!   }
//! }
//! ```


use std::collections::VecDeque;
use std::collections::HashSet;
use std::collections::BinaryHeap;

use std::cmp::Ordering;


/// A node in the graph, made up a any content type `T` and a `Vec` of vertices
pub struct Node<T> {
  /// content can be any type `T`
  pub content: T,
  /// adjacent takes a `Vec` of vertices to adjacent nodes
  pub adjacent: Vec<Vertex>,
}

/// A vertex between two `Node`s with an associated `i32` cost and a target node.
/// `Vertex` derives `Copy`, `Debug`, `Eq` and `PartialEq` and implements `Ord` and
/// `PartialOrd` as we use it ordered compound types.
#[derive(Eq, PartialEq, Debug)]
pub struct Vertex {
  /// cost is defiened as an `ì32`, which might change
  pub cost: i32,
  /// node, an `usize` index of the node at the other end of this vertex
  pub node: usize
}
// The priority queue depends on `Ord`.
// Explicitly implement the trait so the queue becomes a min-heap
// instead of a max-heap.
impl Ord for Vertex {
    fn cmp(&self, other: &Vertex) -> Ordering {
        // Notice that the we flip the ordering here
        other.cost.cmp(&self.cost)
        //self.cost.cmp(&other.cost)
    }
}

// `PartialOrd` needs to be implemented as well.
impl PartialOrd for Vertex {
    fn partial_cmp(&self, other: &Vertex) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}


/// A graph, represeted by as a weighted
/// [Adjacency list](http://en.wikipedia.org/wiki/Adjacency_list) of `Node`s
pub struct Graph<T> {
  /// The underlaying graph represented here with weights as an i32 (should
  /// probably be generic) the graph is represented as an
  /// [Adjacency list](http://en.wikipedia.org/wiki/Adjacency_list) of
  /// Nodes. Nodes themselves are made up of a type T content and a list of adjacent
  /// Vertices
  graph: Vec<Node<T>>
}
impl <T: PartialEq> Graph<T> {
  /// `new` allows for initializing the graph struct with a given adjacency list
  ///
  /// ## Arguments
  /// * `input` - an adjacency list in made out of a `Vec` of Nodes.
  ///    Weights are represented as `i32`:s and can thus be positive or
  ///    negative numbers.
  ///
  /// ## Example
  /// ```
  /// use graphsearch::{Graph, Node, Vertex};
  /// let rawgraph = vec![Node{content: "Helsinki",
  ///                          adjacent: vec![Vertex{cost: 20, node: 1},
  ///                                         Vertex{cost: 50, node: 2},
  ///                                         Vertex{cost: 10, node: 3}]},
  ///                     Node{content: "Turku",
  ///                          adjacent: Vec::new()},
  ///                     Node{content: "Tampere",
  ///                          adjacent: vec![Vertex{cost: 50, node: 6}]},
  ///                     Node{content: "Jyväskylä",
  ///                          adjacent: vec![Vertex{cost: 20, node: 4}]},
  ///                     Node{content: "Oulu",
  ///                          adjacent: vec![Vertex{cost: 20, node: 3},
  ///                                         Vertex{cost: 30, node: 6}]},
  ///                     Node{content: "Rovaniemi",
  ///                          adjacent: Vec::new()},
  ///                     Node{content: "Vasa",
  ///                          adjacent: Vec::new()}];
  /// let g = Graph::new(rawgraph);
  /// ```
  pub fn new(input: Vec<Node<T>>) -> Graph<T> {
    Graph { graph: input }
  }

  /// `search` promises to use a correct method, i.e. one which will return the
  /// _best_ path between `start` and `target` if there is a valid path between them.
  /// Which search method applied is not specified but currently Dijkstras algorithm
  /// is used. The path found is returned as a `VecDeque<usize>` of nodes. The
  /// `VecDeque<usize>` is an optional type as there might not be a path.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an `T` designating the target node
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn search(&self, start: usize, target: T) -> Option<VecDeque<usize>> {
    return self.dijkstra(start, &target)
  }

  /// `search_using_index` promises to use a correct method, i.e. one which will
  /// return the _best_ path between the `start` index and `target` index if there
  /// is a valid path between them. Which search method applied is not specified but
  /// currently Dijkstras algorithm is used. The path found is returned as a
  /// `VecDeque<usize>` of nodes. The `VecDeque<usize>` is an optional type as there
  /// might not be a path.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an `usize` designating the target node, or row in the adjacency list
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn search_using_index(&self, start: usize, target: usize) -> Option<VecDeque<usize>> {
    return self.dijkstra_using_index(start, target)
  }

  /// `breadth_first_search` implements breadth first search from `start` to the
  /// `target` and returns the path found as a `VecDeque<usize>` of nodes. This
  /// is an optional type as there might not be a path.
  ///
  /// **NOTE** as this is breadth first search this search ignores any assigned
  /// weight to nodes.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an `T` designating the target node
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn breadth_first_search(&self, start: usize, target: T) -> Option<VecDeque<usize>> {
    return self.inner_search(start, &target, true)
  }

  /// `breadth_first_search` implements breadth first search from the `start` index to the
  /// `target` and returns the path found as a `VecDeque<usize>` of nodes. This
  /// is an optional type as there might not be a path.
  ///
  /// **NOTE** as this is breadth first search this search ignores any assigned
  /// weight to nodes.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an `usize` designating the target node, or row in the adjacency list
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn breadth_first_search_using_index(&self, start: usize, target: usize) -> Option<VecDeque<usize>> {
    return self.inner_search_using_index(start, target, true)
  }

  /// `depth_first_search` implements depth first search from `start` to the
  /// `target` and returns the path found as a `VecDeque<usize>` of nodes. This
  /// is an optional type as there might not be a path.
  ///
  /// **Note:** as this is depth first search this search ignores any assigned
  /// weight to nodes.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an `T` designating the target node
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn depth_first_search(&self, start: usize, target: T) -> Option<VecDeque<usize>> {
    return self.inner_search(start, &target, false)
  }

  /// `depth_first_search_using_index` implements depth first search from the `start`
  /// index to the `target` index and returns the path found as a `VecDeque<usize>`
  /// of nodes. This is an optional type as there might not be a path.
  ///
  /// **Note:** as this is depth first search this search ignores any assigned
  /// weight to nodes.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an `usize` designating the target node, or row in the adjacency list
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn depth_first_search_using_index(&self, start: usize, target: usize) -> Option<VecDeque<usize>> {
    return self.inner_search_using_index(start, target, false)
  }

  /// `dijkstra` implements Dijkstras algorithm for search from `start` to the
  /// `target` and returns the path found as a `VecDeque<usize>` of nodes. This
  /// is an optional type as there might not be a path.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an ref `T` designating the target node
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn dijkstra(&self, start: usize, target: &T) -> Option<VecDeque<usize>> {
    let mut q = BinaryHeap::new();
    let mut costs: Vec<_> = (0..self.graph.len()).map(|_| std::i32::MAX).collect();
    let mut prev: Vec<usize> = (0..self.graph.len()).map(|_| 0).collect();
    let mut pathfound = false;
    let mut target_index = start;

    costs[start] = 0;
    q.push(Vertex {cost: 0, node: start});

    while !q.is_empty() {
      let v = q.pop();
      match v {
        None => {},
        Some(Vertex {cost, node}) => {
          if &self.graph[node].content == target {
            pathfound = true;
            target_index = node
          }
          if cost > costs[node] { continue; } // we have a better path
          for vert in &self.graph[node].adjacent {
            let next = Vertex { cost: cost+vert.cost, node: vert.node };
            if next.cost < costs[vert.node] {
              costs[vert.node] = next.cost;
              prev[vert.node] = node;
              q.push(next);
            }
          }
        }
      }
    }
    if pathfound { return Some(self.backtrack(prev, start, target_index)) }
    return None
  }

  /// `dijkstra_using_index` implements Dijkstras algorithm for search from the `start`
  /// index to the `target` index and returns the path found as a `VecDeque<usize>`
  /// of nodes. This is an optional type as there might not be a path.
  ///
  /// ## Arguments
  /// * `start`  - an `usize` designating the start node, or row in the adjacency list
  /// * `target` - an `usize` designating the target node, or row in the adjacency list
  ///
  /// ## Returns
  /// Either the found path between start and target as a `VecDeque` of `usize`:s
  /// or `None` if there is no path.
  pub fn dijkstra_using_index(&self, start: usize, target: usize) -> Option<VecDeque<usize>> {
    match self.index_to_node(target) {
      None => { return None }, // target does not exist in the graph
      Some(node) => { return self.dijkstra(start, &node.content) }
    }
  }
  /// `cost_of_path` takes a path returned from any of the search functions and
  /// calculates the cost of the path.
  ///
  /// ## Arguments
  /// * `path` - a borrowed reference to a `VecDeque<usize>` representing a path
  ///            through the graph
  ///
  /// ## Returns
  /// The cost of traversing said path represented as an `i32`
  pub fn cost_of_path(&self, path: &VecDeque<usize>) -> i32 {
    let mut cost = 0;
    for i in (0..path.len()-1) {
      for vert in &self.graph[path[i]].adjacent {
        if vert.node==path[i+1] { cost = cost + vert.cost; }
      }
    }
    return cost
  }

  /// `node_to_index` takes an `Node` (from the graph) and returns its index, if
  /// it exists in the graph.
  ///
  /// **Note:** This operation searches the graph array for the input node and is
  ///           thus an (ammortized) **O(N)** computation.
  ///
  /// ## Arguments
  /// * `node` - The node for which we want an index
  ///
  /// ## Returns
  /// An Optional index, either `None` if the node does not exist in the graph or
  /// `Some(index)`
  pub fn node_to_index(&self, node: Node<T>) -> Option<usize> {
    for i in (0..self.graph.len()) {
      if node.content==self.graph[i].content { return Some(i) }
    }
    return None
  }

  /// `index_to_node` takes an index (from the graph) and returns the node at the
  /// index, if the index is within the bounds of the graph. This is equivalent to
  /// accessing the underlying vector and thus is of **O(1)** complexity
  ///
  /// ## Arguments
  /// * `index` - The `usize` index for which we want to know the corresponding `Node`
  ///
  /// ## Returns
  /// An Optional `Node` if the index supplied is within the bounds of the graph,
  /// `None` otherwise
  pub fn index_to_node(&self, index: usize) -> Option<&Node<T>> {
    if index < self.graph.len() { return Some(&self.graph[index]); }
    return None
  }

  fn inner_search(&self, start: usize, target: &T, bfs: bool) -> Option<VecDeque<usize>> {
    let mut q = VecDeque::new();
    let mut discovered: HashSet<usize> = HashSet::new();
    let mut prev: Vec<usize> = (0..self.graph.len()).map(|_| 0).collect();
    let mut pathfound = false;
    let mut target_index = start;

    q.push_back(start);
    discovered.insert(start);

   while !q.is_empty() {
      let mut v: Option<usize>;
      if bfs {
        v = q.pop_front()
      } else {
        v = q.pop_back();
      }
      match v {
        None => {}, // q is empty
        Some(v) => { // we are working on a new layer
          let node = &self.graph[v];
          if !discovered.contains(&v) { discovered.insert(v); }
          if &node.content == target {
            pathfound = true;
            target_index=v;
          }
          for vertex in &node.adjacent {
            if !discovered.contains(&vertex.node) {
              q.push_back(vertex.node);
              prev[vertex.node]=v; //track prev (v) on i
            }
          }
        }
      }
    }
    if pathfound { return Some(self.backtrack(prev, start, target_index)) }
    return None
  }

  fn inner_search_using_index(&self, start: usize, target: usize, bfs: bool) -> Option<VecDeque<usize>>  {
    match self.index_to_node(target) {
      None => { return None }, // target does not exist in the graph
      Some(node) => { return self.inner_search(start, &node.content, bfs) }
    }
  }

  /// `backtrack` is a simple function for traversing the graph from `target` to
  /// `start` using the path indicated by the `prev` Vec.
  ///
  /// ## Returns
  /// Returns a the path between `start` and `target` as a `VecDeque<usize>`.
  fn backtrack(&self, prev: Vec<usize>, start: usize, target: usize) -> VecDeque<usize> {
      let mut path: VecDeque<usize> = VecDeque::new();
      let mut curr = target;
      // backtrack over the prev array to construct the path
      while curr != start {
        path.push_front(curr);
        curr = prev[curr];
      }
      path.push_front(start);
      return path
  }
}

/* Search tests */

#[test]
fn search_test() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 6;
  let expected_path = vec![0, 3, 4, 6];
  let expected_cost = 60;
  let g = Graph::new(testgraph);
  let res = g.search(start, target);
  match res {
    None => {
      println!("Search returned None");
      assert!(false);
    }
    Some(result) => {
      println!("Search returned something: {:?}", result);
      println!("The cost of path is: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      for i in (0..expected_path.len()) { assert_eq!(result[i], expected_path[i]); }
      assert_eq!(expected_cost, g.cost_of_path(&result));
    }
  }
}

#[test]
fn search_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.search(start, target);

  // The expected return value is None
  match res {
    None => {
      println!("Search returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Search returned something: {:?}", result);
      println!("The returned path cost: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}

#[test]
fn search_using_index_test() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 6;
  let expected_path = vec![0, 3, 4, 6];
  let expected_cost = 60;
  let g = Graph::new(testgraph);
  let res = g.search_using_index(start, target);
  match res {
    None => {
      println!("Search returned None");
      assert!(false);
    }
    Some(result) => {
      println!("Search returned something: {:?}", result);
      println!("The cost of path is: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      for i in (0..expected_path.len()) { assert_eq!(result[i], expected_path[i]); }
      assert_eq!(expected_cost, g.cost_of_path(&result));
    }
  }
}

#[test]
fn search_using_index_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.search_using_index(start, target);

  // The expected return value is None
  match res {
    None => {
      println!("Search returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Search returned something: {:?}", result);
      println!("The returned path cost: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}

/* Breadth first search tests */

#[test]
fn breadth_first_search_test() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 6;
  let expected_path = vec![0, 2, 6];
  let expected_cost = 100;
  let g = Graph::new(testgraph);
  let res = g.breadth_first_search(start, target);
  match res {
    None => {
      println!("Breadth first search returned None");
      assert!(false);
    }
    Some(result) => {
      println!("Breadth first search returned something: {:?}", result);
      println!("The cost of path is: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      for i in (0..expected_path.len()) { assert_eq!(result[i], expected_path[i]); }
      assert_eq!(expected_cost, g.cost_of_path(&result));
    }
  }
}

#[test]
fn breadth_first_search_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.breadth_first_search(start, target);

  // The expected return value is None
  match res {
    None => {
      println!("Breadth first search returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Breadth first search returned something: {:?}", result);
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}

#[test]
fn breadth_first_search_using_index_test() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 6;
  let expected_path = vec![0, 2, 6];
  let expected_cost = 100;
  let g = Graph::new(testgraph);
  let res = g.breadth_first_search_using_index(start, target);
  match res {
    None => {
      println!("Breadth first search returned None");
      assert!(false);
    }
    Some(result) => {
      println!("Breadth first search returned something: {:?}", result);
      println!("The cost of path is: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      for i in (0..expected_path.len()) { assert_eq!(result[i], expected_path[i]); }
      assert_eq!(expected_cost, g.cost_of_path(&result));
    }
  }
}

#[test]
fn breadth_first_search_using_index_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.breadth_first_search_using_index(start, target);

  // The expected return value is None
  match res {
    None => {
      println!("Breadth first search returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Breadth first search returned something: {:?}", result);
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}

/* Depth first tests */

#[test]
fn depth_first_search_test() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 6;
  let expected_path = vec![0, 3, 4, 6];
  let expected_cost = 60;
  let g = Graph::new(testgraph);
  let res = g.depth_first_search(start, target);
  match res {
    None => {
      println!("Depth first search returned None");
      assert!(false);
    }
    Some(result) => {
      println!("Depth first search returned something: {:?}", result);
      println!("The cost of path is: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      for i in (0..expected_path.len()) { assert_eq!(result[i], expected_path[i]); }
      assert_eq!(expected_cost, g.cost_of_path(&result));
    }
  }
}

#[test]
fn depth_first_search_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.depth_first_search(start, target);

  // The expected return value is None
  match res {
    None => {
      println!("Depth first search returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Depth first search returned something: {:?}", result);
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}

#[test]
fn depth_first_search_using_index_test() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 6;
  let expected_path = vec![0, 3, 4, 6];
  let expected_cost = 60;
  let g = Graph::new(testgraph);
  let res = g.depth_first_search_using_index(start, target);
  match res {
    None => {
      println!("Depth first search returned None");
      assert!(false);
    }
    Some(result) => {
      println!("Depth first search returned something: {:?}", result);
      println!("The cost of path is: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      for i in (0..expected_path.len()) { assert_eq!(result[i], expected_path[i]); }
      assert_eq!(expected_cost, g.cost_of_path(&result));
    }
  }
}

#[test]
fn depth_first_search_using_index_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.depth_first_search_using_index(start, target);

  // The expected return value is None
  match res {
    None => {
      println!("Depth first search returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Depth first search returned something: {:?}", result);
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}


/* Djikstra tests */

#[test]
fn dijkstra_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.dijkstra(start, &target);

  // The expected return value is None
  match res {
    None => {
      println!("Dijkstra returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Dijkstra returned something: {:?}", result);
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}

#[test]
fn dijkstra_using_index_test() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 6;
  let expected_path = vec![0, 3, 4, 6];
  let expected_cost = 60;
  let g = Graph::new(testgraph);
  let res = g.dijkstra_using_index(start, target);
  match res {
    None => {
      println!("Dijkstra search returned None");
      assert!(false);
    }
    Some(result) => {
      println!("Dijkstra returned something: {:?}", result);
      println!("The cost of path is: {}", g.cost_of_path(&result));
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      for i in (0..expected_path.len()) { assert_eq!(result[i], expected_path[i]); }
      assert_eq!(expected_cost, g.cost_of_path(&result));
    }
  }
}

#[test]
fn dijkstra_using_index_test_no_valid_path() {
  let testgraph = vec![Node{content: 0, adjacent: vec![Vertex{cost: 20, node: 1}, Vertex{cost: 50, node: 2}, Vertex{cost: 10, node: 3}]},
                       Node{content: 1, adjacent: Vec::new()},
                       Node{content: 2, adjacent: vec![Vertex{cost: 50, node: 6}]},
                       Node{content: 3, adjacent: vec![Vertex{cost: 20, node: 4}]},
                       Node{content: 4, adjacent: vec![Vertex{cost: 20, node: 3}, Vertex{cost: 50, node: 3}, Vertex{cost: 30, node: 6}]},
                       Node{content: 5, adjacent: Vec::new()},
                       Node{content: 6, adjacent: Vec::new()}];
  let start: usize = 0;
  let target: usize = 5; // There is no valid path between 0 and 5
  let g = Graph::new(testgraph);
  let res = g.dijkstra_using_index(start, target);

  // The expected return value is None
  match res {
    None => {
      println!("Dijkstra returned None");
      assert!(true);
    }
    Some(result) => {
      println!("Dijkstra returned something: {:?}", result);
      assert_eq!(result[result.len()-1], target);
      assert_eq!(result[0], start);
      assert!(false);
    }
  }
}