Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions src/FlightRoutesGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;
import edu.greenriver.sdev333.HashSet;

/**
* Created in class 3/13/23, Ken coding
*
* printlns added by me
*
* Importing interface MathSet - can we use this for testing?
*/
public class FlightRoutesGraph {

// two sets needed to model the graph
// 1. a set of vertices (points, nodes) - airports
// 2. a set of edges (connections, lines, relationships) - routes between airports

private class Edge {
private String node1;
private String node2;

public Edge(String from, String to) {
node1 = from;
node2 = to;
}
}

private MathSet<String> nodes; // set of nodes

private MathSet<Edge> edges; // set of edges

//Commented out so Main class would compile
public FlightRoutesGraph() {
nodes = new BSTSet<>();
edges = new HashSet<>(); // must use HashSet here as edges are not comparable
}

public void addNode(String city) {
nodes.add(city);
}

public void addEdge(String city1, String city2) {
Edge connection = new Edge(city1, city2);
edges.add(connection);
}

/*
Edge Set = {
ORD, JFK
JFK, MCO
DEN, PHX
LAS, DEN
DFW, ORD
ATL, JFK
}

IF we are looking for cities connected to Denver, we are looking for 'Adjacency, i.e. neighbors.
*/

/**
* Return cities that are neigbors to given city. There can be more than
* one neighbor, so should return a Set, i.e. MathSet of Strings/neighbors
*
* @param city
*/
public MathSet<String> getNeighbors(String city) {
// create an empty set to hold the results
MathSet<String> neighbors = new BSTSet<>();

// loop through edges and check if city is
// in either node1 or node2
for (Edge e: edges.keys()) {
if (e.node1.equals(city)) {
neighbors.add(e.node2);
}
else if (e.node2.equals(city)) {
neighbors.add(e.node1);
}
}

return neighbors;
}

public static void main(String[] args) {
FlightRoutesGraph g = new FlightRoutesGraph();

// add all the cities first (nodes)
g.addNode("JFK");
g.addNode("ORD");
g.addNode("ATL");
g.addNode("MCO");
g.addNode("DEN");

// add connections beween cities
g.addEdge("ATL", "MCO");
g.addEdge("JFK", "MCO");
g.addEdge("DEN", "ORD");
g.addEdge("ORD", "ATL");
// more to go if you want

// look for direct flights from JFK
MathSet<String> directFromJFK = g.getNeighbors("JFK");
MathSet<String> directFromATL = g.getNeighbors("ATL");

System.out.println(directFromJFK);
System.out.println(directFromATL);

}
}
58 changes: 57 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.MathSet;
import edu.greenriver.sdev333.HashSet;

/**
* Used to test our set implementations. Each class is declared twice to
* test against itself. Uncomment/comment the appropriate lines to test
* a specific class (BSTSet or SeparateChainingHashSet)
*
* @author Paul Woods
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

// Test BSTSet structures
MathSet<String> test1 = new BSTSet<>();
MathSet<String> test2 = new BSTSet<>();

// Test SeparateChainingHashTable structures
//MathSet<String> test1 = new HashSet<>();
//MathSet<String> test2 = new HashSet<>();

System.out.println("isEmpty() - should be true: " + test1.isEmpty());
System.out.println("size() - should be 0: " + test1.size());

System.out.println("adding 6 keys to set ... ");

test1.add("g");
test1.add("q");
test1.add("r");
test1.add("b");
test1.add("k");
test1.add("c");

System.out.println("size() - should be 6: " + test1.size());
System.out.println("isEmpty() - should be false: " + test1.isEmpty());
System.out.println();
System.out.println("contains('r') - should be true: " + test1.contains("r"));
System.out.println("contains('a') - should be false: " + test1.contains("a"));
System.out.println("contains('c') - should be true: " + test1.contains("c"));
System.out.println("contains('z') - should be false: " + test1.contains("z"));
System.out.println();

System.out.println("Creating 2nd set to test MathSet interface functions");

test2.add("z");
test2.add("a");
test2.add("b"); // in test1
test2.add("q"); // in test1
test2.add("j");
test2.add("c"); // in test1

System.out.println("contents of test1: " + test1.toString());
System.out.println("contents of test2: " + test2.toString());
System.out.println();
System.out.println("test1.union(test2): " + test1.union(test2));
System.out.println("test1.intersection(test2): " + test1.intersection(test2));
System.out.println("test1.difference(test2): " + test1.difference(test2));

}
}
Loading