-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectedEdge.java
More file actions
134 lines (124 loc) · 4.19 KB
/
DirectedEdge.java
File metadata and controls
134 lines (124 loc) · 4.19 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
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
/******************************************************************************
* Compilation: javac Edge.java
* Execution: java Edge
* Dependencies: StdOut.java
*
* Immutable weighted edge.
*
******************************************************************************/
/**
* The {@code Edge} class represents a weighted edge that represents a cable in an network
* {@link EdgeWeightedGraph}. Each edge consists of two integers
* (naming the two vertices) and a real-value weight. The data type
* provides methods for accessing the two endpoints of the edge and
* the weight. The natural order for this data type is by
* ascending order of weight.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/43mst">Section 4.3</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*
* @author runyuan yan
*/
public class DirectedEdge implements Comparable<DirectedEdge> {
private final int from;//source
private final int to;//destination
private double latency;//time takes to travel,the actual weight
private int bandWidth;
private int length;
private String material;
private final int COPPER_SPEED = 230000000; //The speed at which a single data packet can be sent across copper wire in meters per second
private final int FIBER_SPEED = 200000000;
/**
* Initializes an edge between vertices {@code v} and {@code w} of
* the given {@code weight}.
*
* @param v one vertex
* @param w the other vertex
* @param weight the weight of this edge
* @throws IllegalArgumentException if either {@code v} or {@code w}
* is a negative integer
* @throws IllegalArgumentException if {@code weight} is {@code NaN}
*/
public DirectedEdge(int from, int to,String material, int length,int bandWidth) {
if (from < 0) throw new IllegalArgumentException("vertex index must be a nonnegative integer");
if (to < 0) throw new IllegalArgumentException("vertex index must be a nonnegative integer");
if (Double.isNaN(length)) throw new IllegalArgumentException("Weight is NaN");
this.from = from;
this.to = to;
this.length = length;
this.bandWidth = bandWidth;
this.material = material;
if(material.equals("copper"))
{
latency = (double) (length * Math.pow(10, 9)/COPPER_SPEED);
}
if(material.equals("optical"))
{
latency = ((double) 1/FIBER_SPEED) * length * Math.pow(10, 9);
}
}
/**
* Returns the weight of this edge.
*
* @return the weight of this edge
*/
public double latency() {
return latency;
}
/**
* Returns source of this edge.
*
* @return either source of this edge
*/
public int from() {
return from;
}
/**
* Returns the destination vertex of the directed edge.
* @return the destination vertex of the directed edge
*/
public int to() {
return to;
}
public int bandWidth()
{
return bandWidth;
}
public int length()
{
return length;
}
public String material()
{
return material;
}
/**
* Compares two edges by weight.
* Note that {@code compareTo()} is not consistent with {@code equals()},
* which uses the reference equality implementation inherited from {@code Object}.
*
* @param that the other edge
* @return a negative integer, zero, or positive integer depending on whether
* the weight of this is less than, equal to, or greater than the
* argument edge
*/
@Override
public int compareTo(DirectedEdge that) {
return Double.compare(this.latency, that.latency);
}
/**
* Returns a string representation of this edge.
*
* @return a string representation of this edge
*/
/**
* Returns a string representation of the directed edge.
* @return a string representation of the directed edge
*/
public String toString() {
return from + "->" + to + " " + String.format("%5.2f", latency);
}
}