Top Banner

of 76

CP7111-Advanced Data Structures Laboratory Manual

Mar 02, 2016

Download

Documents

ahmadhassan306

Advanced Data Structures Lab Manual.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Prepared By TJS ENGINEERING COLLEGE STUDENTSDijkstras AlgorithmAim:Given a graph with appropriate weights for each node, find the single source shortest path using Dijkstras algorithm.

Algorithm:algorithm DijkstraShortestWeightedPath(G, s)_ pre-cond_: G is a weighted (directed or undirected) graph, and s is one of its nodes._ post-cond_: specifies a shortest weighted path from s to each node of G, and d specifiestheir lengths.begind(s) = 0, (s) = _for other v, d(v)=and (v) = nilhandled = notHandled = priority queue containing all nodes. Priorities given by d(v).loop_loop-invariant_: See above.exit when notHandled = let u be a node fromnotHandled with smallest d(u)for each v connected to ufoundPathLength = d(u) +w_u,v_if d(v) > foundPathLength thend(v) = foundPathLength(v) = u(update the notHandled priority queue)end ifend formove u from notHandled to handledend loopreturn _d, _end algorithm

SOURCE CODE: package dijkstrashortestpath;import java.util.InputMismatchException;import java.util.Scanner; public class DijkstraShortestPath{ private boolean settled[]; private boolean unsettled[]; private int distances[]; private int adjacencymatrix[][]; private int numberofvertices; public DijkstraShortestPath(int numberofvertices) { this.numberofvertices = numberofvertices; this.settled = new boolean[numberofvertices + 1]; this.unsettled = new boolean[numberofvertices + 1]; this.distances = new int[numberofvertices + 1]; this.adjacencymatrix = new int[numberofvertices + 1][numberofvertices + 1]; } public void dijkstraShortestPath(int source, int adjacencymatrix[][]) { int evaluationnode; for (int vertex = 1; vertex = maze.length || square.y < 0 || square.y >= maze[square.x].length ) {

return false; }

return ( maze[square.x][square.y] == FREE_CHAR || maze[square.x][square.y] == FINISH_CHAR ); } private void enterSquare ( Point square ) { maze[square.x][square.y] = PATH_CHAR; totalSteps++; }

private void exitSquare ( Point square ) { maze[square.x][square.y] = FREE_CHAR; totalSteps--; } private boolean mazeFinished ( Point location ) { return location.equals(finishLocation); }

public void printMaze() { for ( int i = 0; i < maze.length; i++ ) { for ( int j = 0; j < maze[i].length; j++ ) { System.out.print(maze[i][j]); } System.out.println(); } System.out.println(); }

private Point getStartLocation() { Point startLocation = findChar(START_CHAR); if ( startLocation == null ) { throw new IllegalStateException("Maze has no start square!"); }

return startLocation; }

private Point getFinishLocation() { Point finishLocation = findChar(FINISH_CHAR); if ( finishLocation == null ) { throw new IllegalStateException("Maze has no finish square!"); }

return finishLocation; }

private Point findChar( char c ) { for ( int i = 0; i < maze.length; i++ ) { for ( int j = 0; j < maze[i].length; j++ ) { if ( maze[i][j] == c ) { return new Point(i, j); } } } return null; } public static void main ( String[] args ) { char[][] easyMaze = { { WALL_CHAR, START_CHAR, WALL_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR }, { WALL_CHAR, FREE_CHAR, WALL_CHAR, WALL_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, WALL_CHAR }, { WALL_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR }, { WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR }, { WALL_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR }, { WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, WALL_CHAR }, { WALL_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, FREE_CHAR, FREE_CHAR, WALL_CHAR, FREE_CHAR, FINISH_CHAR }, { WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR } };

MazeSolver s = new MazeSolver(easyMaze); System.out.println("The maze looks like:"); s.printMaze();

s.solve(); }}

OUTPUT:

RESULT:

For a given maze of size 12*12, the program successfully computed the path from source the destination

GRAPH COLORING USING BACKTRACKINGAim:Given a graph, to color using backtracking in java

Algorithm:Given G=(V,E):Compute Degree(v) for all v in V.Set uncolored = V sorted in decreasing order of Degree(v).set currentColor = 0.while there are uncolored nodes:set A=first element of uncoloredremove A from uncoloredset Color(A) = currentColorset coloredWithCurrent = {A}for each v in uncolored:if v is not adjacent to anything in coloredWithCurrent:set Color(v)=currentColor.add v to currentColor.remove v from uncolored.end ifend forcurrentColor = currentColor + 1.end while

SOURCE CODE: import java.io.*;public class GraphColoring{static int [][] G;static int [] x;static int n, m;static boolean found = false;public static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) throws IOException{System.out.println("\t\t\t\tGRAPH COLORING");System.out.print("\nEnter the number of the vertices: ");n = Integer.parseInt(br.readLine());G = new int[n+1][n+1];x = new int[n+1];System.out.print("\nIf edge between the following vertices enter 1 else 0:\n");for(int i=1;i