Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use the Author\'s library to solve the following problem. *Sedgewick and

ID: 3571258 • Letter: P

Question

Please use the Author's library to solve the following problem. *Sedgewick and Wayne* From their Algorithms 4th edition book. This is to be solved in Java. Please make it as simple as possible and try to explain it as well.

The indegreeof a vertex in a digraph is the number of directed edges that point to that vertex. The outdegreeof a vertex in a digraph is the number of directed edges that emanate from that vertex. No vertex is reachable from a vertex of outdegree 0, which is called a sink,a vertex of indegree 0, which is called a source, is not reachable from any other vertex. A digraph where self- loops are allowed andevery vertex has outdegree 1 is called a map(a function from the set of integers from 0 to V 1 onto itself). Write a program Degrees java that implements the following AP public class Degrees Degrees (Digraph G) constructor int indegree(int v) indegree of v int outdegree Cint v) out degree of v Iterable sources() sources Iterable sinks sinks boolean is MapC) is Ga map?

Explanation / Answer


import java.util.LinkedList;
import java.util.List;

/**
* Ex 4.2.7
* public class Degrees
*/
public class Degrees {
   private int[] indegree;
   private int[] outdegree;
   private List<Integer> sources;
   private List<Integer> sinks;

   public Degrees(Digraph G) {               // constructor
       indegree = new int[G.V()];
       outdegree = new int[G.V()];
       sources = new LinkedList<>();
       sinks = new LinkedList<>();
       for (int v = 0; v < G.V(); v++) {
           for (int w : G.adj(v)) {
               indegree[w]++;
               outdegree[v]++;
           }
       }
       for (int v = 0; v < G.V(); v++) {
           if (indegree[v] == 0)
               sources.add(v);
           if (outdegree[v] == 0)
               sinks.add(v);
       }
   }

   public int indegree(int v) {           // indegree of v
       return indegree[v];
   }

   public int outdegree(int v) {           // outdegree of v
       return outdegree[v];
   }

   public Iterable<Integer> sources() {   // sources
       return sources;
   }

   public Iterable<Integer> sinks() {       // sinks
       return sinks;
   }

   public boolean isMap() {               // is G a map?
       for (int v = 0; v < outdegree.length; v++)
           if (outdegree[v] != 1)
               return false;
       return true;
   }
}