CSE/IT 213 - Stacks and Graphs: New Mexico Tech
CSE/IT 213 - Stacks and Graphs: New Mexico Tech
Generics
A generic class is a class that allows you to specify the
data type of one or more fields as a parameter.
General form of a generic class
ClassName<E1,E2,...,En>, where Ei are placeholders for
some reference type.
Use inheritance to limit the types of classes generic
classes could use
public ClassName <T extends P> restricts T to be of class
P and its subclasses
1
extends P>
Stack
What is a stack? Basic data structure.
Lets define an interface that defines stack-ness.
What constitutes stack-ness?
import java.util.*;
class Stack<T> implements StackInterface<T> {
private ArrayList<T> stack;
//constructors
/** default of ArrayList is 10 items */
public Stack() {
4
public T getTop () {
if (!stack.isEmpty())
return stack.get(stack.size() - 1);
else
return null;
}
public T pop () {
if (stack.isEmpty()) //stack contains no elements
return null;
return stack.remove(stack.size() - 1);
}
stack.pop();
}
}
}
37
31
29
23
17
Uses of Stacks
Balanced parenthesis
postfix evaluation
Graphs
Balanced parenthesis
Mathematics, source code, markup languages depend
on balanced parenthesis (or braces, brackets, etc) to
operate correctly.
For example this expression evaluates correctly
[(a + b)2 + ecos(x+y)] sin(e(xy)) + (a b3)
[ () () ] ( () ) ()
while this one doesnt as it has doesnt have a matching
parenthesis
[(a + b)2 + ecos(x+y)] sin(e(xy) + (a b3)
[ () () ] ( () ()
7
input string
{[x+b]+(9[x])}
[x+b]+(9[x])}
x+b]+(9[x])}
+b]+(9[x])}
b]+(9[x])}
]+(9[x])}
+(9[x])}
(9[x])}
9[x])}
[x])}
x])}
])}
)}
}
end of string
action
push {
push [
read x
read t
read b
read ] pop matching left [
read +
push (
read 9
push [
read x
read ] pop matching left [
read ) pop matching left )
read } pop matching left }
empty stack balanced
9
Evaluating postfix
n1 op n2 expressions are called infix, where op represents
an operator and n1, n2 are operands.
n1 n2 op are called postfix or RPN. An advantage RPN
has over infix is there is no need for parenthesis.
infix
(3 + 5) 4
(x y z)/(u + v)
RPN
35 + 4
xy z uv + /
10
Evaluate 3 5 + 4
stack
empty
3
3 5
3
empty
8
8 4
8
empty
32
input string
35 + 4
5 + 4
+4
4
4
4
empty
empty
empty
action
read 3, push 3 onto stack
read 5, push 5 onto stack
read +, pop 5 from stack
pop 3 from stack
push 5 + 3 = 8 onto stack
read 4, push 4 onto stack
read *, pop 4 from stack
pop 8 from stack
push 8 4 = 32 onto stack
done, string is empty
12
Graphs
A graph is a collection of vertices and edges.
Vertices, also called nodes, can have names and other
properties,
Edges are connections between two vertices.
13
Graphs are called weighted if the edges store some information that model some relationship between vertices. For instance edges as roads, one could assign
the miles between vertices (cities).
Representing Graphs
First step is convert vertex names to an integer between
1 and n, where n is the number of vertices. Allows you
to use an array indexing to access vertices.
Then can use an adjacency matrix to represent a graph.
An adjacency matrix is a n n boolean valued matrix.
Where a[i][j] = 1 represents that there is an edge between vertex i and vertex j. a[j][i] is also assigned a 1.
While this adds to storage, makes algorithms easier.
Often assume that there is an edge from each vertex
to itself. So a[i][i] = 1. That is the diagonal of the
matrix is set to 1. In other cases, it may be easier to
assign 0 to the diagonal.
15