CSE/IT 213 - BFS & Linked Lists: New Mexico Tech
CSE/IT 213 - BFS & Linked Lists: New Mexico Tech
Pseudo-code BFS
//Input Graph G = (V,E)
//Output: Graph G with vertices marked with
//consecutive integers in the order they are visted
Mark each vertex v in V with a 0
//0 means vertex unvisited
count <-- 0
for each vertex v in V do
if v is marked with 0
bfs(v)
2
bfs(v)
//visits all the unvisited vertices adjacent to v
//by a path and numbers them in order they
//are visited via the global variable count
count <-- count + 1
mark v with count and initialize a queue with v
while queue is not empty
//front vertex - the vertex first in line in the queue
for each vertex w in V adjacent to the front vertex
if w is marked with 0
count <-- count + 1
add w to the queue
remove front vertex from queue
Linked Lists
Goal: Design a link list in Java
Nodes a node an object that contains data as well as
references to another node.
Nodes have at least two fields one which holds the
address of another node
Nodes are self-referential or recursive data structures
To use:
Node p = new Node("Apple");
Node q = new Node("Google");
p.next = q; //Apple points to Google node
//p.next holds the address of the Google node
import java.util.*;
public class Chain {
private class Node
{
5
public Chain() {
Scanner input = new Scanner (System.in);
String name;
Node q, r;
System.out.print("Enter name: Press <Enter> to end");
name = input.nextLine();
//create first node
front = new Node(name);
q = front;
System.out.print("Enter name: ");
name = input.nextLine();
while(!name.equals("")) {
r = new Node(name); //get new node
q.next = r; //link prev. node
q = r; //move q to new node
System.out.print("Enter name: ");
name = input.nextLine();
}
}
public void printChain()
{
Node q = front;
System.out.println("the cahin of nodes are:");
while (q != null) {
System.out.println(q.data);
q = q.next;
}
}
public static void main(String[] args) {
Chain chain = new Chain();
chain.printChain();
}
}
Inner Classes
Methods of inner class have direct access to variables
and methods of the surrounding outer class.
The methods of the outer class can only access the an
inner class field or or invoke an inner class method only
via an object of the inner class.
This is wrong
public class BadOuter {
private class Inner
{
private String inStr;
}
public void setName(String name) {
inStr = name; //wrong
}
}
The fix
public class GoodOuter {
private class Inner
{
private String inStr;
}
public void setName(String name) {
Inner inner = new Inner();
inner.inStr = name;
}
}
9
10
The interface
public interface ListInterface<E> {
/** insert x into position index */
void add(int index, E x);
/** adds x to the end of the list */
void add(E x);
/**removes all objects from the list */
void clear();
/** returns true if x is a member of the list */
boolean contains(E x);
11
this.data = x;
this.next = null;
}
} //end Node
//instance variable
//list locators
private Node front, rear, current;
private int length; //size of list
public LList() {
this.front = this.rear = this.current = null;
this.length = 0;
}
rear = front;
current = front;
length++;
return;
}
//add at rear of list
if( index == length) {
add(x);
return;
}
//add other place
Node q = front;
for(int i = 0; i < index - 1; i++)
q = q.next;
Node r = q.next;
q.next = p;
p.next = r;
current = front;
length++;
}
rear.next = p;
rear = p;
}
current = front;
length++;
}
/**removes all objects from the list */
public void clear() {
front = rear = null;
length = 0;
}
/** returns true if x is a member of the list */
in get(int index)");
System.exit(0);
}
Node p = front;
for(int i = 0; i< index; i++)
p = p.next;
return p.data;
}
/**returns true if list is empty */
public boolean isEmpty() {
return length == 0;
}
if (p == front)
front = front.next;
if (p == rear)
rear = q;
length--;
return true;
}
/**removes and return the object a position index */
public E remove (int index) {
if (index > this.length) {
System.out.println("index out of range
in remove(int index)");
System.exit(0);
}
Node p = front;
Node q = null;
for (int i =0; i <index; i++) {
q = p; //q trails p by one
p = p.next;
}
if (current == p)
current = p.next;
if (!(q == null)) //if x is in first node q is null
q.next = p.next;
if (p == front)
front = front.next;
if (p == rear)
rear = q;
length--;
return p.data;
}
/** replaces the object at index at position index
* returns the replaced object */
public E set (int index, E x) {
if (index > this.length) {
System.out.println("index out of range
in set(int index)");
System.exit(0);
}
Node p = front;
for(int i = 0; i< index; i++)
p = p.next;
E tmp = p.data;
p.data = x;
return tmp;
}
/**returns the number of objects ccurrently in list */
public int size() {
return length;
}
System.out.println("Error in hasNext");
System.exit(0);
}
E tmp = current.data;
current = current.next;
return tmp;
}
}
0
1
2
3
4
5
6
7
8
9
Time to add to rear of list = 5
10000
9999
9998
9997
9996
9995
9994
9993
9992
9991
9990
Time to add to front of list = 5
list.get(i);
long end = System.currentTimeMillis();
System.out.println("Time for get = " + (end - start));
System.out.println();
start = System.currentTimeMillis();
while(list.hasNext()) {
list.next();
}
end = System.currentTimeMillis();
}
Time for get = 102
Time for hasNext = 1
Why does it take longer for get than hasNext?
get starts at the beginning each time it is called
Node p = front;
for(int i = 0; i< index; i++)
p = p.next;
return p.data;
So going to do 1 + 2 + 3 + 4 + ... + 10000 times.
hasNext() executes only once per call.
15