Find Us On:

# Java Programming - Graph Algorithms

quest5178467

Please help with the attached assignment on graph algorithms. I mainly need the programming section done but theory also would be greatly appreciated. Attachments Tags
Clarifications

## implementation of graph algos: bfs, dijkstra and prim

OutOfBounds

all methods and algos implemented. Attachments
graphsalgos.zip (355 K)

Preview
this.setPreferredSize(dimension); this.pack(); } private class ComputeHandler implements ActionListener { public ComputeHandler() { } public void actionPerformed(ActionEvent e) { } } public static void main(String[] args) throws Exception { if (args.length < 2) {

Preview
public Edge(Vertex source, Vertex target, Double theCost) { sourceVertex = source; targetVertex = target; cost =

Preview
Double cost) { if (!vertices.containsKey(nameU)) addVertex(nameU); if (!vertices.containsKey(nameV)) addVertex(nameV); Vertex sourceVertex = vertices.get(nameU); Vertex targetVertex = vertices.get(nameV); Edge newEdge1 = new Edge(sourceVertex, targetVertex, cost); Edge newEdge2 = new Edge(targetVertex, sourceVertex, cost); sourceVertex.addEdge(newEdge1); targetVertex.addEdge(newEdge2); } static double computeEuclideanCosts(Graph g, String vertexName1, String vertexName2) { Vertex

Preview
, y + VERTEX_RADIUS/2); } public void paintEdge(Graphics g, Vertex u, Vertex v) { int x1 = Math.round(xFactor * (float)u.posX + (float)MARGIN_X); int y1 = Math.round(yFactor * (float)u.posY + (float)MARGIN_Y); int x2 = Math.round(xFactor * (float)v.posX + (float)MARGIN_X); int y2 = Math.round(yFactor *

Preview
String edge = scanner.next(); if (!edge.trim().isEmpty() && edge.trim() != null) { String vertexAttributes[] = edge.split(","); String vertexName1 = vertexAttributes[0].trim(); String vertexName2 = vertexAttributes[1].trim(); double cost = Graph.computeEuclideanCosts(g, vertexName1, vertexName2); g.addUndirectedEdge(vertexName1, vertexName2, cost); } } scanner.close(); return

Preview
.println("java TestBfs ttrvertices.txt ttredges.txt NewYork LosAngeles"); System.exit(0); } String vertexFile = args[0]; String edgeFile = args[1]; Graph g = MapReader.readGraph(vertexFile, edgeFile);

Preview
static void main(String[] args) throws Exception { if (args.length < 4) { System.out.println("Please enter folowing format"); System.out .println("java TestBfs <vertices_txt_file> <edges_txt_file> <City1> <City2>");

Preview
< 3) { System.out.println("Please enter folowing format"); System.out .println("java TestBfs <vertices_txt_file> <edges_txt_file> <StartCity>"); System.out .println("java TestBfs ttrvertices.txt ttredges.txt NewYork"); System.exit(0); }

Preview
* @param vertexName * a unique identifier for this vertex. * @param x * the x coordinate for this vertex * @param y * the y coordinate for this vertex */ public Vertex(String vertexName, int x, int y) { name = vertexName; adjacent = new LinkedList<Edge>(); posX = x;

Preview

Preview

Preview

Preview

Screenshots

## Graph Algorithms - Partial Solution

mariobeka

As discussed, this is not a perfect solution.

implemented and full working are:
-addEdge(String s, String t, Double cost) :: method
-addUndirectedEdge(String s, String t, Double cost) :: method
-Graph readGraph(String vertexfile, String edgefile) :: method
-doBfs(String s) :: method
-doDijkstra(String s) :: method
-Graph getWeightedShortestPath(String s, String t) :: method
-TestDijkstra :: class

not implemented are:
-void doPrim(String s) :: method
-Graph getMinimumSpanningTree(String s) :: method
-TestPrim:: class
-Graph getUnweightedShortestPath(String s, String t):: method
Attachments
Archive.zip (5 K)
Preview
} private class ComputeHandler implements ActionListener { public ComputeHandler() { } public void actionPerformed(ActionEvent e) { }

Preview
this.cost = theCost; } public Edge(Vertex source, Vertex target) { this(source, target, 1.0); } public int compareTo(Edge other) { return

Preview
the connection,mark Vertex v = abj.targetVertex; // everyone if (!v.visited) {// if is not mark v.visited = true;// mark it v.prev = n; q.add(v);// add to q } } } System.out.println("BFS is done"); } public Graph getUnweightedShortestPath(String s, String t) { //TODO return null; } public void doDijkstra(String source) {

Preview
GraphPanel extends JPanel { // graph layout parameters static final int VERTEX_RADIUS = 10; static final int SPACE = 3; static final int MARGIN_X = 50; static final int MARGIN_Y = 50; // scale factors float xFactor, yFactor;

Preview
class MapReader { public static void main(String[] args) throws FileNotFoundException { if (args.length < 2){ System.out.println("java MapReader ttrvertices.txt ttredges.txt"); } Graph graph = readGraph(args[0], args[1]); graph.printAdjacencyList(); DisplayGraph display = new DisplayGraph(graph); display.setVisible(true); } public static Graph readGraph(String vertexFile, String edgeFile) { Graph graph = new Graph(); BufferedReader brV =

Preview
< 4){ System.out.println("java TestDijkstra ttrvertices.txt ttredges.txt NewYork LosAngeles"); } Graph graph; MapReader

Preview
< 4){ System.out.println("java TestDijkstra ttrvertices.txt ttredges.txt NewYork LosAngeles"); } Graph graph; MapReader

Preview
false; scratch = 0; } /** * Construct a new vertex containing an adjacency list. * @param vertexName a unique identifier for this vertex. */ public Vertex(String vertexName) { this(vertexName, 0, 0); } /** * Retrieve the list of edges connected to this vertex. * @return a list of

Screenshots