-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDVAlgorithm.java
125 lines (86 loc) · 3.76 KB
/
DVAlgorithm.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package project3ccn;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
*
* @author shrirupa chowdhury
* @author lizzy thomas
*/
public class DVAlgorithm {
public static DistanceVector Result ;
public static DistanceVector doDV(DistanceVector DV, DistanceVector UPDATE)
{
/*
dv.source = A
dv.vector = B,(8,C) --- meaning distance to B is 8 through C
*/
Result = new DistanceVector();
HashMap<String, String> current = DV.vector;
String currentName = DV.source;
HashMap<String, String> update = UPDATE.vector;
String updateName = UPDATE.source;
Double dtoNode = DistanceVector.getDistance(current.get(updateName)) ;
////iterate over current vector
// Set keysone = current.keySet();
////CHECK IF THE NEIGHBORS DISTANCE HAS CHANGED AND UPDATE DISTANCES
// if(dtoNode!=DistanceVector.getDistance(update.get(currentName)))
// {
// Double newDist = DistanceVector.getDistance(update.get(currentName));
// for(Iterator i = keysone.iterator();i.hasNext();)
// {
// String keyone = (String) i.next();
// String via = DistanceVector.getThrough(current.get(keyone));
// Double updatedDist = newDist+ DistanceVector.getDistance(update.get(keyone));
// if(via.equalsIgnoreCase(updateName)){
// current.put(keyone, DistanceVector.createValue(updatedDist, keyone));
// }
// }
// }
Set keys = update.keySet();
for(Iterator i = keys.iterator();i.hasNext();)
{
String key = (String) i.next();
if(!current.containsKey(key))//if that key is not present in the distance vector then add it
{
Double distance = dtoNode + DistanceVector.getDistance(update.get(key));
String via = updateName;
current.put(key,DistanceVector.createValue(distance, via));
}else{
if(DistanceVector.getDistance(current.get(key)) > (dtoNode + DistanceVector.getDistance(update.get(key)) ))
{
Double distance = dtoNode + DistanceVector.getDistance(update.get(key));
String via = updateName;
current.put(key,DistanceVector.createValue(distance, via));
}
}
}
Result.source = currentName;
Result.vector = current;
return Result;
}
public static void main(String[] args)
{
DistanceVector A = new DistanceVector();
A.setSource("A");
HashMap<String,String> dva = new HashMap<>();
dva.put("D", "7,A");
dva.put("B", "4,A");
dva.put("A", "0,A");
A.vector = dva;
DistanceVector B = new DistanceVector();
B.setSource("B");
HashMap<String,String> dvb = new HashMap<>();
dvb.put("C", "3,B");
dvb.put("D", "1,B");
dvb.put("A", "4,B");
dvb.put("B", "0,B");
B.vector = dvb;
DistanceVector.display(doDV(A, B));
}
}