| Line | |
|---|
| 1 | // License: GPL v3 or later courtesy of author Kevin Wayne
|
|---|
| 2 | package edu.princeton.cs.algs4;
|
|---|
| 3 |
|
|---|
| 4 | /*************************************************************************
|
|---|
| 5 | * Compilation: javac DirectedEdge.java
|
|---|
| 6 | * Execution: java DirectedEdge
|
|---|
| 7 | *
|
|---|
| 8 | * Immutable weighted directed edge.
|
|---|
| 9 | *
|
|---|
| 10 | *************************************************************************/
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * The <tt>DirectedEdge</tt> class represents a weighted edge in an directed graph.
|
|---|
| 14 | * <p>
|
|---|
| 15 | * For additional documentation, see <a href="http://algs4.cs.princeton.edu/44sp">Section 4.4</a> of
|
|---|
| 16 | * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | public class DirectedEdge {
|
|---|
| 20 | private final int v;
|
|---|
| 21 | private final int w;
|
|---|
| 22 | private final double weight;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Create a directed edge from v to w with given weight.
|
|---|
| 26 | */
|
|---|
| 27 | public DirectedEdge(int v, int w, double weight) {
|
|---|
| 28 | this.v = v;
|
|---|
| 29 | this.w = w;
|
|---|
| 30 | this.weight = weight;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Return the vertex where this edge begins.
|
|---|
| 35 | */
|
|---|
| 36 | public int from() {
|
|---|
| 37 | return v;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Return the vertex where this edge ends.
|
|---|
| 42 | */
|
|---|
| 43 | public int to() {
|
|---|
| 44 | return w;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Return the weight of this edge.
|
|---|
| 49 | */
|
|---|
| 50 | public double weight() { return weight; }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Return a string representation of this edge.
|
|---|
| 54 | */
|
|---|
| 55 | @Override
|
|---|
| 56 | public String toString() {
|
|---|
| 57 | return v + "->" + w + " " + String.format("%5.2f", weight);
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.