input
stringlengths
10
828
output
stringlengths
5
107
public string edge type return dir forward
default to having nodes with complex record shapes
public void output graph flow network fn output graph fn false
output to stdout a dotty representation without cut
public void output hashtable ipoint list iline segment result for ipoint ip result key set list iline segment ilss result get ip system out println ip for iline segment ils ilss system out println ils system out println
convenient helper class to format the output from intersection algorithms
protected void populate iterator edge info edges info new edge info num vertices num vertices note that initially the flow is set to zero while edges has next edge info ei edges next ei flow 0 for the sake of sanity clear out info ei start ei end ei
helper method to populate edges
public ford fulkerson flow network network search method this network network this search method method
construct instance to compute maximum flow across the given network
public void output graph flow network fn boolean include cut system out println compute dotty fn include cut
output to stdout a dotty representation and include the min cut
public void add forward edge info ei forward append ei
add the given edge into the list of forward edges
protected flow network array int source index int sink index int num vertices super source index sink index num vertices
minimal constructor for use by subclasses
public void add backward edge info ei backward append ei
add the given edge into the list of backward edges
public boolean compute boolean augmented false while search method find augmenting path network vertices process path network vertices augmented true return augmented
compute the maximal flow for the given flow network
protected igraph entity get int idx return nodes get idx
get the node identified by the integer index
protected void set int idx igraph entity ge nodes put idx ge
set the node to be associated with the integer index
public void output int max pairs get flow system out println there are max pairs pairs matched pr output
show the matching
protected void assign vertices flow network fn for int i 0 i fn num vertices i final int id i igraph entity node new igraph entity public string node label return id nodes put i node now visit each one for int i 0 i fn num vertices i dd visit node nodes get i
assign node entities for all nodes in the graph according to their
public pair object element object match this element element this match match
construct the element match relationship
public boolean equals object o if o null return false if o instanceof pair pair p pair o return p element equals element p match equals match return false
standard equals method
public string to string return element match
standard to string method
public edge info int start int end int cap this start end cap 0
construct edge info from start end vertices with given capacity
public void clear int col int row cells col row empty
clears the cell at given location
public edge info int start int end int cap int cost this start start this end end this capacity cap this cost cost
construct edge info from start end vertices with given capacity
private boolean contains object set object element for object i set if i equals element return true return false
helper method to determine if element is a member of the array
public string to string return start end flow capacity cost
reasonable to string
public flow network int num vertices int src index int sinkindex this source index src index this sink index sinkindex this num vertices num vertices this vertices new vertex info num vertices
store relevant information about the flow network graph
public optimized int n int s int t iterator edge info edges have superclass initialize first super n s t queue new int n queue size n capacity new int n n flow new int n n previous new int n visited new int n initially the flow is set to zero pull info from input while edges has next edge info ei edges next capacity ei start ei end ei capacity
load up information for this network problem
public int hash code return start end capacity something efficient but who knows how effective
support hash code protocol
public optimized flow network int num vertices int src index int sink index iterator edge info edges n num vertices capacity new int n n flow new int n n previous new int n visited new int n source src index sink sink index note that initially the flow is set to zero pull info from input while edges has next edge info ei edges next capacity ei start ei end ei capacity
construct an instance of the flow network problem using optimized array
public boolean set int col int row char mark if cells col row empty return false ok to move cells col row mark return true
sets the cell at given location to contain mark
public hyperpoint imulti point pt int d this dimensionality pt dimensionality values new double d for int i 1 i d i values i 1 pt get coordinate i
construct when given an imulti point
public int compute int source int sink int max flow 0 while find augmenting path source sink max flow process path source sink return max flow
compute the maximal flow for the given flow network
public hypercube int dimension this dimension dimension this lows new double dimension this highs new double dimension
construct an n dimensional hypercube with origin coordinates
public hyperpoint double vals int d this dimensionality vals length values new double d for int i 1 i d i values i 1 vals i 1
construct when given a raw array of double values
public hyperpoint string s array list string al new array list string string tokenizer st new string tokenizer s while st has more tokens al add st next token dimensionality al size values new double dimensionality for int i 1 i dimensionality i values i 1 double value of al get i 1
construct hyperpoint when given a comma separated string of doubles
public boolean equals object o if o null return false if o instanceof imulti point imulti point imp imulti point o if imp dimensionality dimensionality return false if imp instanceof hyperpoint hyperpoint other hyperpoint imp for int i 1 i dimensionality i if floating point same other values i 1 values i 1 return false else for int i 1 i dimensionality i if floating point same imp get coordinate i values i 1 return false must be the same return true nope return false
supports the equals checking of imulti point objects
public hypercube ihypercube cube this dimension cube dimensionality this lows new double dimension this highs new double dimension for int i 1 i dimension i this lows i 1 cube get left i this highs i 1 cube get right i
fill in values for this hypercube drawn from the ihypercube parameter
public string to string string builder sb new string builder for int i 0 i dimensionality i sb append values i return sb substring 0 sb length 1
reasonable to string implementation
public boolean is clear for int c 0 c max c c for int r 0 r max r r found one if is clear c r return false nada return true
return true if entire board is empty
public hypercube double lows double highs dimension lows length if lows length highs length throw new illegal argument exception lows and highs arrays do not contain the same number of dimensions if dimension 2 throw new illegal argument exception hypercube can only be created with dimensions of 2 and higher this lows new double dimension this highs new double dimension for int i 0 i dimension i this lows i lows i this highs i highs i
construct an n dimensional hypercube
public double get coordinate int d return values d 1
all coordinates are 1 based
public double node e e this value e
construct node from the given element
public double distance imulti point imp if imp dimensionality dimensionality throw new illegal argument exception distance computation can only be performed between points with the same dimension double sum 0 optimize for two hyperpoints if imp instanceof hyperpoint hyperpoint other hyperpoint imp for int i 1 i dimensionality i double d other values i 1 values i 1 sum d d else for int i 1 i dimensionality i double v imp get coordinate i sum v values i 1 v values i 1 return math sqrt sum
return the euclidean distance between the given imulti point
public e value return value
return value stored with the node
public int dimensionality return dimension
return the dimensionality of this hypercube
public int hash code long bits java lang double double to long bits values 0 for int i 2 i dimensionality i bits java lang double double to long bits values i 1 31 use java awt 2 dpoint hashcode function return int bits int bits 32
reasonable hash code
public void prev double node e p prev p
modifies the previous link for this node
public void next double node e n next n
modifies the next link for this node
public double linked list iterator double linked list e elist base elist current elist head count 0 safe to remove false
constructor for the iterator over the list
public boolean same board tic tac toe board board2 for int i 0 i filters length i boolean is same true for int k 0 k 9 k note that the 0th one is the base covers equals if get filters 0 k col filters 0 k row board2 get filters i k col filters i k row is same false break if is same return true return false
determine if two boards are the same including rotations and reflections
public e next count safe to remove true e ret val current value current current next return ret val
return next element in the iteration
public list iterator list e l current l head
constructor for iterator takes the list as parameter
public double get left int d return lows d 1
return the left coordinate in the given dimension
public node e e value e
node constructed with value
public void remove if safe to remove throw new illegal state exception invalid invocation of remove if count 1 base remove first else snip out current prev prev current base size count safe to remove false
remove the most recent element retrieved by the
public void remove throw new unsupported operation exception remove not supported for list iterator
remove operation not supported for list objects
public boolean search t collection t target null is never included in the collection if target null return false int low 0 high collection length 1 while low high int ix low high 2 int rc target compare to collection ix if rc 0 target is less than collection i high ix 1 else if rc 0 target is greater than collection i low ix 1 else found the item return true return false
search for target in collection
public boolean sequential search t collection t t for t item collection if item equals t return true return false
apply the brute force sequential search algorithm to search the
public boolean search k k int h hash method hash k linked list entry k v list linked list entry k v table h if list null return false int sz list size for int i 0 i sz i entry k v e list get i if e key equals k return true nope return false
search for the desired value in the hash table
public void set left int d double value lows d 1 value
set the left coordinate in the given dimension
public boolean sequential search iterable t collection t t iterator t iter collection iterator while iter has next if iter next equals t return true return false
apply the brute force sequential search algorithm to search the
public void append e e if e null throw new illegal argument exception unable to append null to a list node e node new node e e if head null head tail node else tail next node tail tail next size
append element to the end of the list
public standard hash int table size this table size table size
hash needs to know size of the table
public list hash table reporter list hash table v ht this table ht
load up the hash table for this reporter
public e contains e e if head null return null node e n head while n null if n value equals e return n value n n next return null
determine membership by returning element if found
public simple hash int table size this table size table size
hash needs to know size of the table
public double linked list comparator null size 0
construct double linked list with no comparator defaults to append on insert
private void calculate slot usage num entries 0 for int i 0 i table table length i linked list v list linked list v table table i if list null num entries list size int hash table i list size
compute usages based upon the table
public int hash string s int h 0 for int i 0 i s length i h h 1 s char at i if h 0 h 0 h return h table size
computed hash function evaluates every character in the given string
public double get right int d return highs d 1
return the right coordinate in the given dimension
public e remove if head null throw new no such element exception nothing in list e result head value only one in the list take care of it if head tail head tail null else simply update head head head next size return result
remove element from the front of the list and return it
private int min collisions int min table table size for int i int hash table if i min min i return min
return minimum number of collisions in the hash table
public void concat list e list silently ignore requests to concatenate null or empty lists if list null list size 0 return if head null head list head tail list tail size list size return attach in node e new tail list tail tail next list head tail new tail size list size
concatenate a list to the end of our list
private int max collisions int max 0 for int i int hash table if i max max i return max
return maximum number of collisions in the hash table
public boolean search v v int h hash method hash v linked list v list linked list v table h if list null return false return list contains v
search for the desired value in the hash table
public void set right int d double value highs d 1 value
set the right coordinate in the given dimension
public void load iterator v it pull each value from the iterator and add while it has next v v it next add v v
bulk load elements into the hash table from the iterator
public void remove iterator k it pull each value from the iterator and add while it has next remove it next
bulk remove elements from the hash table from the iterator of key values
public string node label right threaded binary node t thread get next left 2 right 3 sentinel string builder sb new string builder sb append sb append value get value sb append if thread null sb append thread get value return sb to string
expose threaded value in the node label
public void add v k add k k
list hash table objects add elements who are themselves keys
public boolean has next return internal has next
delegate request to the internal iterator
public int hash code long bits java lang double double to long bits lows 0 for int i 1 i dimension i bits java lang double double to long bits lows i 1 31 for int i 1 i dimension i bits java lang double double to long bits highs i 1 31 use java awt 2 dpoint hashcode function return int bits int bits 32
meaningful hashcode function
public double linked list comparator e comp if comp null comparator new comparator e public int compare e o1 e o2 return 1 else comparator comp size 0
construct double linked list with comparator
public boolean equals object o if o null return false if o instanceof ihypercube ihypercube ihc ihypercube o if ihc dimensionality dimension return false check each dimension and leave right away on failure for int i 1 i dimension i double x ihc get left i if floating point same x lows i 1 return false x ihc get right i if floating point same x highs i 1 return false everything passes return true return false
determine equality by comparing coordinates on each dimension
public void visit binary node k parent binary node k n if not yet seen visit node only have to check parent for root if parent null nodes contains parent visit node parent if nodes contains n visit node n if parent null visit edge parent n
visit parent child pair by visiting both nodes then add the edge
public boolean intersects double raw point if raw point length dimension throw new illegal argument exception unable to determine intersection between hypercube dimension dimension and point dimension raw point length check each dimension and leave right away on failure for int i 0 i dimension i if raw point i lows i raw point i highs i return false passed all checks return true
determine intersection among all point coordinates in raw optimized form
public void validate int new size 0 double node e p head double node e check head iterate through entire list and advance check by two while p is advanced by one if they ever are equal then we have an infinite list while p null p p next new size if check null check check next if check null check check next if check null check p throw new illegal state exception double linked list has circular reference size new size
if links have been manually modified then we must validate the size
public boolean intersects imulti point p if p dimensionality dimension throw new illegal argument exception unable to determine intersection between hypercube dimension dimension and point dimension p dimensionality check each dimension and leave right away on failure for int i 1 i dimension i double x p get coordinate i if x lows i 1 x highs i 1 return false passed all checks return true
determine intersection among all point coordinates
public string node label left 2 right 3 sentinel string builder sb new string builder sb append sb append value value sb append return sb to string
node label for binary node
public void right balanced binary node k v new right right new right
set the right child
public boolean contains ihypercube h throws illegal argument exception if dimension h dimensionality throw new illegal argument exception unable to check containment for hypercubes of different dimensions check each dimension and leave right away on failure for int i 1 i dimension i find target bounds double inner left h get left i double inner right h get right i is inner left inner right wholly contained within our left right if get left i inner left inner left inner right inner right get right i try next dimension else return false fails immediately passed all checks return true
determine if the hypercube wholly contains the given hypercube h
public void left balanced binary node k v new left left new left
set the left child
protected void set root binary node t new root root new root
helper method to properly set the root for the tree
public e contains e e if e null return null if head null return null double node e n head while n null if n value equals e return n value n n next return null
determine membership by returning element if found
public boolean member t value if value null throw new illegal argument exception binary tree cannot store null values empty tree not found if root null return false compare and locate in proper location binary node t node root while node null int c value compare to node value if c 0 return true if c 0 search to the left node node get left son else node node get right son not found return false
determine if the given value occurs in the tree
public boolean intersects ihypercube h throws illegal argument exception if dimension h dimensionality throw new illegal argument exception unable to check containment for hypercubes of different dimensions check each dimension and leave right away on success for int i 1 i dimension i find target bounds double inner left h get left i double inner right h get right i no intersection potential try next dimension if inner right get left i continue if inner left get right i continue return true failed to have a single intersection return false
determine if the hypercube intersects the given hypercube h
public int compare to t o return 1
never allow proper comparison
private void append e e if head null head last new double node e e else double node e new node new double node e e last next new node new node prev last last new node size
append the given value to the end of this list
public string to string string builder sb new string builder for int d 1 d dimension d sb append get left d get right d if d dimension sb append sb append return sb to string
reasonable to string method
public void concat double linked list e list silently ignore requests to concatenate null if list null return if head null head list first last list last return if list head null return double node e new tail list last list head prev last last next list head last new tail size list size
concatenate the two lists
protected void set root right threaded binary node t new root artificial root left new root root new root
helper method to properly set the root for the tree
public void insert inode n stack push n
insert pushes the element onto the stack
public boolean remove double node e node if head node remove first return true if last node remove last return true double node e prev node prev prev next node next if prev next null prev next prev prev size return true
given double node already known to exist in the list remove it