Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package algs 4 2 ; import java.util.Iterator; import java.util.LinkedList; import algs 1 3 . Queue; import stdlib. * ; public class MyEuler { / /
package algs;
import java.util.Iterator;
import java.util.LinkedList;
import algsQueue;
import stdlib.;
public class MyEuler
local copy of the graph
private Queue adj;
private int E;
private boolean isEulerian true;
@SuppressWarningsunchecked
public MyEulerDigraph G
create local copy of graph
adj new QueueGV;
for int v ; v GV; v
adjv new Queue;
for int w : Gadjv
adjvenqueuew;
get initial number of edges
E GE ;
if E
isEulerian true;
return;
find starting vertex with at least one edge
int s ;
while adjsisEmpty
s;
TODO
public Iterable tour
TODO
return null;
public boolean isEulerian
return isEulerian;
public static boolean isTourDigraph G Iterable tour
@SuppressWarningsunchecked
LinkedList adj new LinkedListGV;
int E GE ;
for int v ; v GV; v
adjv new LinkedList;
for int w : Gadjv
adjvaddw;
if tour null
return E ;
Iterator it tour.iterator ;
if ithasNext
return E ;
int s itnext;
int v s;
while ithasNext
int w itnext;
if adjvcontainsw
adjvremoveInteger w;
E;
v w;
else
throw new Error;
if v s throw new Error;
if E throw new Error;
return v s && E ;
public static Digraph inOutEqual int V int E
random graph of V vertices and approximately E edges
with indegreev outdegreev for all vertices
Digraph G new DigraphV;
int indegree new intV;
int outdegree new intV;
int deficit ;
for int i ; i E deficit; i
int v StdRandom.uniformV;
int w StdRandom.uniformV;
if v w i; continue;
GaddEdgev w;
if outdegreev indegreev deficit;
else deficit;
outdegreev;
if indegreew outdegreew deficit;
else deficit;
indegreew;
while deficit
int v StdRandom.uniformV;
int w StdRandom.uniformV;
if v w continue;
if outdegreev indegreev continue;
if indegreew outdegreew continue;
GaddEdgev w;
if outdegreev indegreev deficit;
else deficit;
outdegreev;
if indegreew outdegreew deficit;
else deficit;
indegreew;
return G;
public static void mainString args
args new String "datatinyDGtxt; NO
args new String "datatinyCGtxt; NO
args new String "datatinyDGeulertxt; YES
args new String "datatinyDGeulertxt; YES
args new String "datatinyDGeulertxt; YES
args new String "datatinyDGeulertxt; YES
args new String "datatinyDGeulertxt; NO
args new String "datatinyDGeulertxt; YES
args new String "datatinyDGeulertxt; NO
args new String "datatinyDGeulertxt; YES
args new String "datatinyDGeulertxt; YES
args new String;
Digraph G;
if argslength
In in new Inargs;
G DigraphGenerator.fromInin;
else
int V Integer.parseIntargs;
int E Integer.parseIntargs;
while true
G inOutEqualVE;
if new KosarajuSharirSCCGcount
break;
StdOut.printlnG;
MyEuler euler new MyEulerG;
if eulerisEulerian
for int v : eulertour
StdOut.printv ;
StdOut.println;
else
StdOut.printlnNot eulerian";
if isTourGeulertour StdOut.printlnNot a tour";
MyEuler euler new MyEulerG;
if eulerisEulerian
for int v : eulertour
StdOut.printv ;
StdOut.println;
else
StdOut.printlnNot eulerian";
if isTourGeulertour StdOut.printlnNot a tour";
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started