Skip to content

Latest commit

 

History

History
54 lines (37 loc) · 945 Bytes

Ex_4_1_04.md

File metadata and controls

54 lines (37 loc) · 945 Bytes
title date draft tags categories
Algorithm4 Java Solution 4.1.04
2020-01-28 06:59:15 +0800
false
JAVA
TECH
archives

4.1.04

Problem:

4.1.4 Add a method hasEdge() to Graph which takes two int arguments v and w and returns true if the graph has an edge v-w, false otherwise.

Solution:

2020-02-04_000.jpg

code:

Ex_4_1_04.java

  public static void main(String[] args) {
    Ex_4_1_04 e = new Ex_4_1_04();
    _Graph g = new _Graph(new In("algdata/tinyGex2.txt"));
    PrintUtil.printSepLine("original");
    StdOut.println(g);
    int v = 2, w = 3;
    e._showHasEdge(g, v, w);
    v = 2;
    w = 9;
    e._showHasEdge(g, v, w);

//    2-3 true
//    2-9 false

  }

  private void _showHasEdge(_Graph g, int v, int w) {
    StdOut.printf("%d-%d %s\n", v, w, g.hasEdge(v, w));
  }

Reference: