Adopted and summarized from Columbia University COMS W3251 Professor Bauer's class
protected Set<Node> marked;
protected Graph graph;
public DepthFirstSearch(Graph graphToSearch) {
marked = new HashSet<Node>();
graph = graphToSearch;
}
public boolean dfs(Node start, String elementToFind) {
if (start.getElement().equals(elementToFind)) {
return true;
} else {
marked.add(start);
for (Node neighbor : graph.getNodeNeighbors(start)) {
if (!marked.contains(neighbor) &&
dfs(neighbor, elementToFind)
return true;
}
return false;
}
}
Queue q
q.enqueue(start)
Set visited
while (q is not empty):
u = q.dequeue()
for each v adjacent to u:
if (v is not in visited):
q.enqueue(v)
public void unweighted_shortest_paths(V start) {
LinkedList<Vertex> queue = new LinkedList<>();
Vertex startv = vertices.get(start);
startv.cost = 0 ;
startv.discovered = true;
queue.addLast(startv); // add start vertex
while (queue.size() > 0 ) { // as long as the queue is not empty
Vertex u = queue.pollFirst(); // Visit u
System.out.println(u);
for (Edge uv : u.adjacent ){ // Discover v
Vertex v = uv.target;
if (!v.discovered) { // if v has not been seen before
v.cost = u.cost + 1; // set cost
v.prev = u; // set reference to previous vertex on shortest path
v.discovered = true;
queue.addLast(v); // add v to the queue.
}
}
}
}
public int bfsDistance(Node start, string elementToFind) {
HashMap<Node, Integer> distances = new HashMap<Node, Integer>();
Queue<Node> toExplore = new LinkedList<Node>();
Set<Node> marked = new HashSet<Node>();
marked.add(start);
toExplore.add(start);
distances.put(start, 0);
while (!toExplore.isEmpty()) {
Node current = toExplore.remove();
for (Node neighbor : graph.getNodeNeighbors(current)) {
if (!marked.contains(neighbor)) {
distances.put(neighbor, distances.get(current) + 1);
if (neighbor.getElement().equals(elementToFind)) {
return distances.get(neighbor);
}
marked.add(neighbor);
toExplore.add(neighbor);
}
}
}
return -1;
}
protected Map<V, Vertex> vertices;
private void compute_indegrees() {
for (Vertex u : vertices.values()) {
for (Edge uv : u.adjacent) {
Vertex v = uv.target;
v.indegree += 1;
}
}
}
public List<V> topo_sort() {
ArrayList<V> sort = new ArrayList<>();
LinkedList<Vertex> queue = new LinkedList<>();
// compute indegrees
compute_indegrees();
// Add all vertices with indegree 0 to queue.
for (Vertex u : vertices.values()) {
if (u.indegree == 0)
queue.addLast(u);
}
// Main loop of topological sort
while (queue.size() > 0) {
Vertex u = queue.pollFirst(); // dequeue first element
sort.add(u.name);
for (Edge uv : u.adjacent) {
Vertex v = uv.target;
if (--v.indegree == 0)
queue.addLast(v);
}
}
return sort;
}
for all v:
v.cost = Double.POSITIVE_INFINITY
v.visited = false
v.prev = null
start.cost = 0
PriorityQueue q
q.insert(start)
while (q is not empty):
u = q.pollMin() // visit vertex u
u.visited = true
// discover and relax vertex v
for each v adjacent to u:
if not v.visted:
c = u.cost + cost(u,v)
if (c < v.cost):
v.cost = c
v.prev = u
q.insert(v)
for all v:
v.cost = Double.POSITIVE_INFINITY
v.visited = false
v.prev = null
start.cost = 0
PriorityQueue q
q.insert(start)
while (q is not empty):
u = q.pollMin() // visit vertex u
u.visited = true
// discover and relax vertex v
for each v adjacent to u:
if not v.visted:
if (cost(u,v) < v.cost):
v.cost = cost(u,v)
v.prev = u
q.insert(v)
public void buildHeap( ) {
// start evaluating heap property from the lowest level
for( int i = currentSize / 2; i > 0; i-- )
percolateDown( i );
}
private void percolateDown( int hole ) {
int child;
T tmp = array[ hole ];
boolean done = false;
while (!done && hole * 2 <= currentSize) {
// Select the index of the smaller child
child = hole * 2; // left child
if( child != currentSize && // is there a right child?
array[ child + 1 ].compareTo( array[ child ] ) < 0 ) // right child < left child?
child++; // if so, let child point to the right child index
if( array[ child ].compareTo( tmp ) < 0 ) {
array[ hole ] = array[ child ];
hole = child;
}
else
done=true;
}
array[ hole ] = tmp;
}
(hash(x) + f(i)) % TableSize
If
TableSize
is prime, then the firstTableSize/2
cells visited by quadratic probing are distinct.
Therefore we can always find an empty cell if the table is at most half full.
f(i) = i * hash2(x)
hash2(x) = R - (x % R)