-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaiveAlgorithm.java
More file actions
70 lines (55 loc) · 2.86 KB
/
NaiveAlgorithm.java
File metadata and controls
70 lines (55 loc) · 2.86 KB
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
// Jodi Hieronymus - CPE 400 Final Project - Fall 2022
// Contains all of the necessary features to sort using Naive approach.
import java.util.List;
import java.util.ArrayList;
public class NaiveAlgorithm {
Router parentRouter;
List<Router> knownRouters = new ArrayList<Router>(); // List of all routers the router running the algorithm knows
public NaiveAlgorithm(Router parentRouter) {
this.parentRouter = parentRouter;
}
public void setKnownRouters(List<Router> knownRouters) {
this.knownRouters = knownRouters;
}
// Prints routers that are discovered as options
private void printOptions (List<Router> options) {
System.out.print("--> Routing options for " + parentRouter.getMACAddress() + " (" + options.size() + " available): ");
for (Router router : options) {
System.out.print(router.getMACAddress() + " ");
}
System.out.println();
}
// (Unused - for multidirectional links) Utility function that returns the routers the parent router has a direct link to that have path to destination
public List<Router> findLinkRouters(int destMAC, List<Integer> knowsDestination, List<Integer> doesntKnowDestination, List<Integer> querySources, List<Integer> pathSoFar) {
List<Router> knowsDest = new ArrayList<Router>();
for (Router router : knownRouters) {
// Only check if hasn't been checked, hasn't been to yet
if (!knowsDestination.contains(router.getMACAddress()) && !doesntKnowDestination.contains(router.getMACAddress())) {
Router knows = router.knowsRouter(destMAC, knowsDestination, doesntKnowDestination, querySources, pathSoFar);
if (knows != null) {
System.out.println("Router " + router.getMACAddress() + " knows " + destMAC);
knowsDest.add(router);
}
}
}
return knowsDest;
}
public Router naive(int destMAC, List<Integer> knowsDestination, List<Integer> doesntKnowDestination, List<Integer> querySources, List<Integer> pathSoFar) {
// Create list of routers that are on the path to the destination
// List<Router> routersToDest = findLinkRouters(destMAC, knowsDestination, doesntKnowDestination, querySources, pathSoFar);
List<Router> routersToDest = parentRouter.getKnownRouters();
printOptions(routersToDest);
// Find minimum cost link
Integer minCost = Integer.MAX_VALUE;
Router minRouter = null;
for (Router option : routersToDest) {
int cost = parentRouter.getCost(option);
if ((cost < minCost) && (option.getIsActive())) {
minCost = cost;
minRouter = option;
}
}
System.out.println("... Cost to " + minRouter.getMACAddress() + ": " + minCost);
return minRouter;
}
}