//binary heap
package com.lucky.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* Data structure to support following operations
* extracMin - O(logn)
* addToHeap - O(logn)
* containsKey - O(1)
* decreaseKey - O(logn)
* getKeyWeight - O(1)
*
*
*/
public class BinaryMinHeap<T> {
private List<Node> allNodes = new
ArrayList<>();
private Map<T,Integer> nodePosition = new
HashMap<>();
public class Node {
int weight;
T key;
}
/**
* Checks where the key exists in heap or
not
*/
public boolean containsData(T key){
return
nodePosition.containsKey(key);
}
/**
* Add key and its weight to they
heap
*/
public void add(int weight,T key) {
Node node = new
Node();
node.weight =
weight;
node.key = key;
allNodes.add(node);
int size =
allNodes.size();
int current = size -
1;
int parentIndex =
(current - 1) / 2;
nodePosition.put(node.key, current);
while (parentIndex
>= 0) {
Node parentNode = allNodes.get(parentIndex);
Node currentNode = allNodes.get(current);
if (parentNode.weight > currentNode.weight) {
swap(parentNode,currentNode);
updatePositionMap(parentNode.key,currentNode.key,parentIndex,current);
current = parentIndex;
parentIndex = (parentIndex - 1) / 2;
} else {
break;
}
}
}
/**
* Get the heap min without extracting the
key
*/
public T min(){
return
allNodes.get(0).key;
}
/**
* Checks with heap is empty or not
*/
public boolean empty(){
return allNodes.size()
== 0;
}
/**
* Decreases the weight of given key to
newWeight
*/
public void decrease(T data, int
newWeight){
Integer position =
nodePosition.get(data);
allNodes.get(position).weight = newWeight;
int parent = (position
-1 )/2;
while(parent >=
0){
if(allNodes.get(parent).weight >
allNodes.get(position).weight){
swap(allNodes.get(parent), allNodes.get(position));
updatePositionMap(allNodes.get(parent).key,allNodes.get(position).key,parent,position);
position = parent;
parent = (parent-1)/2;
}else{
break;
}
}
}
/**
* Get the weight of given key
*/
public Integer getWeight(T key) {
Integer position =
nodePosition.get(key);
if( position == null )
{
return null;
} else {
return allNodes.get(position).weight;
}
}
/**
* Returns the min node of the heap
*/
public Node extractMinNode() {
int size =
allNodes.size() -1;
Node minNode = new
Node();
minNode.key =
allNodes.get(0).key;
minNode.weight =
allNodes.get(0).weight;
int lastNodeWeight =
allNodes.get(size).weight;
allNodes.get(0).weight =
lastNodeWeight;
allNodes.get(0).key =
allNodes.get(size).key;
nodePosition.remove(minNode.key);
nodePosition.remove(allNodes.get(0));
nodePosition.put(allNodes.get(0).key, 0);
allNodes.remove(size);
int currentIndex =
0;
size--;
while(true){
int left = 2*currentIndex + 1;
int right = 2*currentIndex + 2;
if(left > size){
break;
}
if(right > size){
right = left;
}
int smallerIndex = allNodes.get(left).weight <=
allNodes.get(right).weight ? left : right;
if(allNodes.get(currentIndex).weight >
allNodes.get(smallerIndex).weight){
swap(allNodes.get(currentIndex), allNodes.get(smallerIndex));
updatePositionMap(allNodes.get(currentIndex).key,allNodes.get(smallerIndex).key,currentIndex,smallerIndex);
currentIndex = smallerIndex;
}else{
break;
}
}
return minNode;
}
/**
* Extract min value key from the
heap
*/
public T extractMin(){
Node node =
extractMinNode();
return node.key;
}
private void printPositionMap(){
System.out.println(nodePosition);
}
private void swap(Node node1,Node
node2){
int weight =
node1.weight;
T data =
node1.key;
node1.key =
node2.key;
node1.weight =
node2.weight;
node2.key = data;
node2.weight =
weight;
}
private void updatePositionMap(T data1, T
data2, int pos1, int pos2){
nodePosition.remove(data1);
nodePosition.remove(data2);
nodePosition.put(data1,
pos1);
nodePosition.put(data2,
pos2);
}
public void printHeap(){
for(Node n :
allNodes){
System.out.println(n.weight + " " + n.key);
}
}
public static void main(String args[]){
BinaryMinHeap<String> heap = new
BinaryMinHeap<String>();
heap.add(10,
"Pramila");
heap.add(5,
"Roy");
heap.add(6,
"NTF");
heap.add(2,"AFR");
heap.decrease("Pramila",
1);
heap.printHeap();
heap.printPositionMap();
}
}
//Graph class for vertices , adding edges and all
package com.lucky.graph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Graph<T>{
private List<Edge<T>>
allEdges;
private Map<Long,Vertex<T>>
allVertex;
boolean isDirected = false;
public Graph(boolean isDirected){
allEdges = new
ArrayList<Edge<T>>();
allVertex = new
HashMap<Long,Vertex<T>>();
this.isDirected =
isDirected;
}
public void addEdge(long id1, long id2){
addEdge(id1,id2,0);
}
//This works only for directed graph because for
undirected graph we can end up
//adding edges two times to allEdges
public void addVertex(Vertex<T>
vertex){
if(allVertex.containsKey(vertex.getId())){
return;
}
allVertex.put(vertex.getId(), vertex);
for(Edge<T> edge :
vertex.getEdges()){
allEdges.add(edge);
}
}
public Vertex<T> addSingleVertex(long
id){
if(allVertex.containsKey(id)){
return allVertex.get(id);
}
Vertex<T> v = new
Vertex<T>(id);
allVertex.put(id,
v);
return v;
}
public Vertex<T> getVertex(long id){
return
allVertex.get(id);
}
public void addEdge(long id1,long id2, int
weight){
Vertex<T> vertex1
= null;
if(allVertex.containsKey(id1)){
vertex1 = allVertex.get(id1);
}else{
vertex1 = new Vertex<T>(id1);
allVertex.put(id1, vertex1);
}
Vertex<T> vertex2
= null;
if(allVertex.containsKey(id2)){
vertex2 = allVertex.get(id2);
}else{
vertex2 = new Vertex<T>(id2);
allVertex.put(id2, vertex2);
}
Edge<T> edge =
new Edge<T>(vertex1,vertex2,isDirected,weight);
allEdges.add(edge);
vertex1.addAdjacentVertex(edge, vertex2);
if(!isDirected){
vertex2.addAdjacentVertex(edge, vertex1);
}
}
public List<Edge<T>>
getAllEdges(){
return allEdges;
}
public Collection<Vertex<T>>
getAllVertex(){
return
allVertex.values();
}
public void setDataForVertex(long id, T
data){
if(allVertex.containsKey(id)){
Vertex<T> vertex = allVertex.get(id);
vertex.setData(data);
}
}
@Override
public String toString(){
StringBuffer buffer =
new StringBuffer();
for(Edge<T> edge :
getAllEdges()){
buffer.append(edge.getVertex1() + " " + edge.getVertex2() + " " +
edge.getWeight());
buffer.append("\n");
}
return
buffer.toString();
}
}
class Vertex<T> {
long id;
private T data;
private List<Edge<T>> edges = new
ArrayList<>();
private List<Vertex<T>>
adjacentVertex = new ArrayList<>();
Vertex(long id){
this.id = id;
}
public long getId(){
return id;
}
public void setData(T data){
this.data = data;
}
public T getData(){
return data;
}
public void addAdjacentVertex(Edge<T> e,
Vertex<T> v){
edges.add(e);
adjacentVertex.add(v);
}
public String toString(){
return
String.valueOf(id);
}
public List<Vertex<T>>
getAdjacentVertexes(){
return
adjacentVertex;
}
public List<Edge<T>>
getEdges(){
return edges;
}
public int getDegree(){
return
edges.size();
}
@Override
public int hashCode() {
final int prime =
31;
int result = 1;
result = prime * result
+ (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() !=
obj.getClass())
return false;
Vertex other = (Vertex)
obj;
if (id !=
other.id)
return false;
return true;
}
}
class Edge<T>{
private boolean isDirected = false;
private Vertex<T> vertex1;
private Vertex<T> vertex2;
private int weight;
Edge(Vertex<T> vertex1, Vertex<T>
vertex2){
this.vertex1 =
vertex1;
this.vertex2 =
vertex2;
}
Edge(Vertex<T> vertex1, Vertex<T>
vertex2,boolean isDirected,int weight){
this.vertex1 =
vertex1;
this.vertex2 =
vertex2;
this.weight =
weight;
this.isDirected =
isDirected;
}
Edge(Vertex<T> vertex1, Vertex<T>
vertex2,boolean isDirected){
this.vertex1 =
vertex1;
this.vertex2 =
vertex2;
this.isDirected =
isDirected;
}
Vertex<T> getVertex1(){
return vertex1;
}
Vertex<T> getVertex2(){
return vertex2;
}
int getWeight(){
return weight;
}
public boolean isDirected(){
return isDirected;
}
@Override
public int hashCode() {
final int prime =
31;
int result = 1;
result = prime * result
+ ((vertex1 == null) ? 0 : vertex1.hashCode());
result = prime * result
+ ((vertex2 == null) ? 0 : vertex2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() !=
obj.getClass())
return false;
Edge other = (Edge)
obj;
if (vertex1 == null)
{
if (other.vertex1 != null)
return false;
} else if
(!vertex1.equals(other.vertex1))
return false;
if (vertex2 == null)
{
if (other.vertex2 != null)
return false;
} else if
(!vertex2.equals(other.vertex2))
return false;
return true;
}
@Override
public String toString() {
return "Edge
[isDirected=" + isDirected + ", vertex1=" + vertex1
+ ", vertex2=" + vertex2 + ", weight=" + weight + "]";
}
}
//dijkstra
package com.lucky.graph;
import java.util.HashMap;
import java.util.Map;
public class DijkstraShortestPath {
public Map<Vertex<Integer>,Integer> shortestPath(Graph<Integer> graph, Vertex<Integer> sourceVertex){
//heap + map data
structure
BinaryMinHeap<Vertex<Integer>> minHeap = new
BinaryMinHeap<>();
//stores shortest
distance from root to every vertex
Map<Vertex<Integer>,Integer> distance = new
HashMap<>();
//stores parent of
every vertex in shortest distance
Map<Vertex<Integer>, Vertex<Integer>> parent =
new HashMap<>();
//initialize all
vertex with infinite distance from source vertex
for(Vertex<Integer> vertex : graph.getAllVertex()){
minHeap.add(Integer.MAX_VALUE, vertex);
}
//set distance of
source vertex to 0
minHeap.decrease(sourceVertex, 0);
//put it in map
distance.put(sourceVertex, 0);
//source vertex
parent is null
parent.put(sourceVertex,
null);
//iterate till heap
is not empty
while(!minHeap.empty()){
//get the min value from heap node which has vertex and distance of
that vertex from source vertex.
BinaryMinHeap<Vertex<Integer>>.Node heapNode =
minHeap.extractMinNode();
Vertex<Integer> current = heapNode.key;
//update shortest distance of current vertex from source
vertex
distance.put(current, heapNode.weight);
//iterate through all edges of current vertex
for(Edge<Integer> edge : current.getEdges()){
//get the adjacent vertex
Vertex<Integer> adjacent = getVertexForEdge(current,
edge);
//if heap does not contain adjacent vertex means adjacent vertex
already has shortest distance from source vertex
if(!minHeap.containsData(adjacent)){
continue;
}
//add distance of current vertex to edge weight to get distance of
adjacent vertex from source vertex
//when it goes through current vertex
int newDistance = distance.get(current) + edge.getWeight();
//see if this above calculated distance is less than current
distance stored for adjacent vertex from source vertex
if(minHeap.getWeight(adjacent) > newDistance) {
minHeap.decrease(adjacent, newDistance);
parent.put(adjacent, current);
}
}
}
return distance;
}
private Vertex<Integer>
getVertexForEdge(Vertex<Integer> v, Edge<Integer>
e){
return
e.getVertex1().equals(v) ? e.getVertex2() : e.getVertex1();
}
public static void main(String args[]){
Graph<Integer>
graph = new Graph<>(false);
/*graph.addEdge(0, 1,
4);
graph.addEdge(1, 2,
8);
graph.addEdge(2, 3,
7);
graph.addEdge(3, 4,
9);
graph.addEdge(4, 5,
10);
graph.addEdge(2, 5,
4);
graph.addEdge(1, 7,
11);
graph.addEdge(0, 7,
8);
graph.addEdge(2, 8,
2);
graph.addEdge(3, 5,
14);
graph.addEdge(5, 6,
2);
graph.addEdge(6, 8,
6);
graph.addEdge(6, 7,
1);
graph.addEdge(7, 8,
7);*/
graph.addEdge(1, 2,
5);
graph.addEdge(2, 3,
2);
graph.addEdge(1, 4,
9);
graph.addEdge(1, 5,
3);
graph.addEdge(5, 6,
2);
graph.addEdge(6, 4,
2);
graph.addEdge(3, 4,
3);
DijkstraShortestPath
dsp = new DijkstraShortestPath();
Vertex<Integer>
sourceVertex = graph.getVertex(1);
Map<Vertex<Integer>,Integer> distance =
dsp.shortestPath(graph, sourceVertex);
System.out.print(distance);
}
}
In JAVA Write a method which performs Dijkstra's algorithm on a Graph from a given stating...
Algirithems and data structure D. Given is the following Graph, a. Use Dijkstra's Algorithm to find Minimum Spanning Graph. Assume "A" as start Node. b. What is the shortest distance from node A to node H2. c.What is the shortest distance from node A to node G?
Consider the problem of finding the shortest paths in a weighted directed graph using Dijkstra's algorithm. Denote the set of vertices as V, the number of vertices as |V|, the set of edges as E, and the number of edges as |E|. Answer the following questions.Below is a pseudo-code of the algorithm that computes the length c[v] of the shortest path from the start node s to each node v. Answer code to fill in the blank _______ .
Algorithm Question 5. Below is a graph with edge lengths. Apply Dijkstra's algorithm to find the shortest paths, starting at vertex A, to all other vertices. Write down the sequence in which the edges are chosen, breaking ties by using vertices at the same length in alphabetic orde. 3 Ga 2 5. Below is a graph with edge lengths. Apply Dijkstra's algorithm to find the shortest paths, starting at vertex A, to all other vertices. Write down the sequence in...
I found this primMST java class source code online for the prim algorithm. It was accompanied by another class called BinaryMinHeap, which is used in the primMST class. I copied and pasted the primMST code in NetBeans, and it gave me error labels in the text editor for three classes that were used in the primMST class. They are class Edge, Graph, and Vertex. There was another error for class List, but I fixed that by using the import statement...
Use Dijkstra's algorithm to determine the shortest path from vertex a to every other vertex in the following graph. Draw your steps on your own draft paper using notation as described in class (you do not need to submit this), then clearly identify and list the following in the text field below: (1) Which edges are included in the SSP; in the format of (vertex1, vertex 2, weight), for example (a, b, 7),(a, c, 9), ... (2) The order and...
Instructions Write a program in Java that implements the A* algorithm to find a path from any two given nodes. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan method for calculating the heuristic. Remember: your heuristic function...
Use the example Graph.javaPreview the document class and Edge.javaPreview the document class as a starting point for this assignment, you'll need to make your own main class to create an instance of the Graph class. For this assignment we are going to create a map of a city subway system using a Graph data structure. The Metro Station Map that you'll use for this assignment is here: Assignment 9 - Metro Station Map.PNG Enter all the information from the Metro...
Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...
Write a function which performs Breadth First Search on the given graph. Also write in which order the nodes are visited. Class Node { public int value; public Node[] neighbors; public boolean visited; public Node(int num) { neighbors = new Node[5]; visited = False; data = num } } 40 20 50 70 10 30 60 40 20 50 70 10 30 60
Consider the java Graph class below which represents an undirected graph in an adjacency list. How would you add a method to delete an edge from the graph? // Exercise 4.1.3 (Solution published at http://algs4.cs.princeton.edu/) package algs41; import stdlib.*; import algs13.Bag; /** * The <code>Graph</code> class represents an undirected graph of vertices * named 0 through V-1. * It supports the following operations: add an edge to the graph, * iterate over all of the neighbors adjacent to a vertex....