I have a hard time to figure out one error after assigning
int evaluationNode = getMinDistances();
settled.add(evaluationNode);
checkNeighbours(evaluationNode);
The error show type mismatch: connot convert from Node to int. I am appreciated if anyone can help me this. Below is a complete code.
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Comparator;
public class DijkstraPriorityQueue
{
private int distances[];
private Set<Integer> settled;
private PriorityQueue<Node> priorityQueue;
private int number_of_nodes;
private int adjacencyMatrix[][];
public DijkstraPriorityQueue(int number_of_nodes)
{
this.number_of_nodes = number_of_nodes;
distances = new int[number_of_nodes + 1];
settled = new HashSet<Integer>();
priorityQueue = new PriorityQueue<Node>(number_of_nodes,new Node());
adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
}
public void dijkstra_algorithm(int adjacency_matrix[][], int source)
{
int evaluationNode;
for (int i = 1; i <= number_of_nodes; i++)
for (int j = 1; j <= number_of_nodes; j++)
adjacencyMatrix[i][j] = adjacency_matrix[i][j];
for (int i = 1; i <= number_of_nodes; i++)
{
distances[i] = Integer.MAX_VALUE;
}
priorityQueue.add(new Node(source, 0));
distances[source] = 0;
while (!priorityQueue.isEmpty())
{
evaluationNode = getMinDistances();
settled.add(evaluationNode);
evaluateNeighbours(evaluationNode);
}
}
private int getMinDistances()
{
int node = priorityQueue.remove();
return node;
}
private void checkNeighbours(int evaluationNode)
{
int edgeDistance = -1;
int newDistance = -1;
for (int destinationNode = 1; destinationNode <= number_of_nodes; destinationNode++)
{
if (!settled.contains(destinationNode))
{
if (adjacencyMatrix[evaluationNode][destinationNode] != Integer.MAX_VALUE)
{
edgeDistance = adjacencyMatrix[evaluationNode][destinationNode];
newDistance = distances[evaluationNode] + edgeDistance;
if (newDistance < distances[destinationNode])
{
distances[destinationNode] = newDistance;
}
priorityQueue.add(new Node(destinationNode,distances[destinationNode]));
}
}
}
}
public static void main(String[] args)
{
int adjacency_matrix[][];
int number_of_vertices;
int source = 0;
Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Enter the source ");
source = scan.nextInt();
DijkstraPriorityQueue dijkstrasPriorityQueue = new DijkstraPriorityQueue(number_of_vertices);
dijkstrasPriorityQueue.dijkstra_algorithm(adjacency_matrix, source);
System.out.println("The Shorted Path to all nodes are ");
for (int i = 1; i <= dijkstrasPriorityQueue.distances.length - 1; i++)
{
System.out.println(source + " to " + i + " is " + dijkstrasPriorityQueue.distances[i]);
}
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}
class Node implements Comparator<Node>
{
public int node;
public int cost;
public Node()
{
}
public Node(int node, int cost)
{
this.node = node;
this.cost = cost;
}
#Override
public int compare(Node node1, Node node2)
{
if (node1.cost < node2.cost)
return -1;
if (node1.cost > node2.cost)
return 1;
return 0;
}
}
In the getMinDistances method you are calling
int node = priorityQueue.remove();
But the priority queue contains Node objects, and not int values.
Maybe you wanted something like
private int getMinDistances()
{
Node node = priorityQueue.remove();
return node.getDistance();
}
but this is something that can not be answered by the debugging cloud (aka stackoverflow).
In order to use the PriorityQueue, you have to implement MinPQ extending the PriorityQueue and you'll also need a Comparator between nodes that returns the Node with a minimum distance in the MinPQ.
Take a look here for more details.
Related
I am solving this challenge: https://open.kattis.com/problems/virtualfriends
My solution seems to be working but kattis's test cases are running too slowly so I was wondering how I can improve code efficiency. I am using a custom made union-find structure to do this, storing "friends" into a treemap to reference.
import java.util.*;
public class virtualfriends {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testcases = scan.nextInt();
scan.nextLine();
for (int i= 0; i < testcases; i++) {
int numFriendships = scan.nextInt();
scan.nextLine();
TreeMap<String , Integer> map = new TreeMap<String , Integer>();
int cnt = 0;
UF unionFind = new UF(50000);
for (int j = 0; j < numFriendships; j++)
{
String p1 = scan.next();
String p2 = scan.next();
if (!map.containsKey(p1)) map.put(p1, cnt++);
if (!map.containsKey(p2)) map.put(p2, cnt++);
unionFind.unify(map.get(p1), map.get(p2));
System.out.printf("%d\n", unionFind.getSetSize(map.get(p2)));
}
}
}
static class UF{
private int[] id, setSize;
private int numSets;
public UF(int size) {
id = new int[size] ;
setSize = new int[size];
numSets = size;
for(int i = 0 ; i < size ; ++i) {
id[i] = i;
setSize[i] = 1;
}
}
int find(int i )
{
int root = i;
while (root != id[root]) {
root = id[root];
}
while (i != root) {
int newp = id[i];
id[i] = root;
i = newp;
}
return root;
}
boolean isConnected(int i , int j) {
return find(i) == find(j);
}
int getNumSets() {
return numSets;
}
int getSetSize(int i) {
return setSize[find(i)];
}
boolean isSameSet(int i, int j) {
return find(i) == find(j);
}
void unify(int i, int j)
{
int root1 = find(i);
int root2 = find(j);
if (root1 == root2) return;
if (setSize[root1] < setSize[root2])
{
setSize[root2] += setSize[root1];
id[root1] = root2;
} else {
setSize[root1] += setSize[root2];
id[root2] = root1;
}
numSets--;
}
}
}
I have created hashtable to solve one of the problem on hackerearth using java.
link:https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/mind-palaces-3/
My java solution is able to pass all the test cases.
Now with same logic ,I am creating solution in C.
But by solution in C does not pass all test cases.
I am learning C.
Please help me to find the problem in C code.
Thanks.
My Java Code:
package hashtable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MindPalces {
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = br.readLine();
String strArr[] = string.split(" ");
int n = Integer.parseInt(strArr[0]);
int m = Integer.parseInt(strArr[1]);
SinglyLinkLists hashTable[] = new SinglyLinkLists[337];
long array[][] = new long[n][m];
for (int i = 0; i < n; i++) {
String string1 = br.readLine();
String[] innerArr = string1.split(" ");
for (int j = 0; j < m; j++) {
array[i][j] = Long.parseLong((innerArr[j]));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
long data = array[i][j];
if (data < 0) {
data = data * -1;
}
int index = Hashs.hashFunction(data);
if (hashTable[index] == null) {
SinglyLinkLists linkList = new SinglyLinkLists();
linkList.insertAtEnd(i, j, array[i][j]);
hashTable[index] = linkList;
} else {
SinglyLinkLists linkList = hashTable[index];
linkList.insertAtEnd(i, j, array[i][j]);
}
}
}
int q = Integer.parseInt(br.readLine());
for (int i = 0; i < q; i++) {
long val = Long.parseLong(br.readLine());
long val1 = val;
if (val < 0) {
val = val * -1;
}
int index = Hashs.hashFunction(val);
if (hashTable[index] == null) {
System.out.println("-1" + " " + "-1");
} else {
SinglyLinkLists linkList = hashTable[index];
NOde list = linkList.getNode(val1);
if (null != list) {
System.out.println(list.getI1() + " " + list.getJ1());
} else {
System.out.println("-1" + " " + "-1");
}
}
}
}
}
class Hashs {
public static int hashFunction(long key) {
return (int) (key % 337);
}
}
class NOde {
int i1 = 0;
int j1 = 0;
NOde link = null;
long number = 0;
public NOde() {
link = null;
i1 = 0;
j1 = 0;
number = 0;
}
public NOde(NOde node, int i1, int j1, long number) {
this.i1 = i1;
this.j1 = j1;
link = node;
this.number = number;
}
public NOde getLink() {
return link;
}
public void setLink(NOde link) {
this.link = link;
}
public int getI1() {
return i1;
}
public void setI1(int i1) {
this.i1 = i1;
}
public int getJ1() {
return j1;
}
public void setJ1(int j1) {
this.j1 = j1;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
}
class SinglyLinkLists {
NOde start = null;
NOde end = null;
int size = 0;
public SinglyLinkLists() {
start = null;
end = null;
size = 0;
}
public void insertAtEnd(int i1, int j1, long number) {
NOde nptr = new NOde(null, i1, j1, number);
size++;
if (start == null) {
start = nptr;
end = start;
} else {
end.setLink(nptr);
end = nptr;
}
}
// Function to display elements
public NOde getNode(long val) {
if (start.getNumber() == val) {
return start;
}
NOde ptr = start;
ptr = start.getLink();
while (null != ptr && ptr.getLink() != null) {
if (ptr.getNumber() == val) {
return ptr;
}
ptr = ptr.getLink();
}
if (null != ptr && ptr.getNumber() == val) {
return ptr;
}
return null;
}
}
My C Code:
#include<stdio.h>
#include <stdlib.h>
struct Node
{
int i1;
int j1;
long inputVal;
struct Node * next;
};
struct hash
{
struct Node * head;
};
struct hash *hashTable=NULL;
struct Node *createNode(int i,int j,long number)
{
struct Node *list;
list = (struct Node *) malloc(sizeof(struct Node));
list->i1 = i;
list->j1 = j;
list->inputVal=number;
list->next = NULL;
return list;
};
int main()
{
int m=0,n=0;
scanf("%d%d",&m,&n);
hashTable=(struct hash *)calloc(337,sizeof(struct Node));
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
long data=0;
long copy=0;
scanf("%ld",&data);
copy=data;
if(data<0)
{
data=data*-1;
}
int index=hashFunction(data);
struct Node *newnode = createNode(i,j,copy);
struct Node *myNode = hashTable[index].head;
if (!myNode)
{
hashTable[index].head=newnode;
}
else
{
newnode->next=hashTable[index].head;
hashTable[index].head=newnode;
}
}
}
int q=0;
scanf("%d",&q);
for(int i=0; i<q; i++)
{
long val=0;
long copy=0;
int boolean=0;
scanf("%ld",&val);
copy=val;
if(val<0)
{
val=val*-1;
}
int index=hashFunction(val);
struct Node *myNode = hashTable[index].head;
if(!myNode)
{
printf("%d %d\n",-1,-1);
}
else
{
while (myNode->next!= NULL)
{
if (myNode->inputVal==copy)
{
boolean=1;
printf("%d %d\n",myNode->i1,myNode->j1);
break;
}
myNode = myNode->next;
}
if(myNode->inputVal==copy &&!boolean)
{
boolean=1;
printf("%d %d\n",myNode->i1,myNode->j1);
}
if(!boolean)
{
printf("%d %d\n",-1,-1);
}
}
}
return 0;
}
int hashFunction(long data)
{
return (int)(data%337);
}
Sample Input:
5 5
-993655555 -758584352 -725954642 -696391700 -649643547
-591473088 -568010221 -432112275 -421496588 -351507172
-323741602 -232192004 -30134637 -369573 100246476
156824549 174266331 392354039 601294716 763826005
768378344 802829330 818988557 992012759 999272829
10
156824549
-758584352
-993655555
601294716
-696391700
802829330
-993655555
-232192004
392354039
-568010221
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
public class TSPNearestNeighbour {
{
private final Stack<Integer> stack;
private int numberOfNodes;
public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public void tsp(int adjacencyMatrix[][])
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int[] visited = new int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i,cost=0;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "\t");
while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes)
{
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{
if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
cost=cost+adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print( dst + "\t");
minFlag = false;
continue;
}
stack.pop();
}
System.out.println("total cost" +cost);
}
public static void main(String args[])
{
int number_of_nodes;
Scanner scanner = null;
try
{
System.out.println("Enter the number of nodes in the graph");
scanner = new Scanner(System.in);
number_of_nodes = scanner.nextInt();
int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
System.out.println("Enter the adjacency matrix");
for (int i = 1; i <= number_of_nodes; i++)
{
for (int j = 1; j <= number_of_nodes; j++)
{
adjacency_matrix[i][j] = scanner.nextInt();
}
}
for (int i = 1; i <= number_of_nodes; i++)
{
for (int j = 1; j <= number_of_nodes; j++)
{
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{
adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
scanner.close();
}
}
> illegal start of expression in the line:
> **private final Stack<Integer> stack;**
You have 2 open braces
public class TSPNearestNeighbour { {
remove one and may be you get your code compiled
Any hint to avoid getting time limit exceeded for this problem :_
http://www.spoj.com/problems/BUGLIFE/
how could i optimize this code
is there a faster method for reading input
these are my methods for reading input :_
http://ideone.com/GtSIAC
my solution :_
import java.io.*;
import java.util.*;
public class buglife {
static int first;
static ArrayList[] graph = new ArrayList[2000];
static int[] colour = new int[2000];
static int[] queue = new int[2000];
static int start;
static int index;
static int current;
public static boolean bfs(int f) {
for (int p = 0; p < first; p++) {
if (colour[p] != f && colour[p] != f + 1) {
int x;
start = 0;
index = 0;
queue[index++] = p;
colour[p] = f;
while (start < index) {
current = queue[start++];
for (int i = 0; i < graph[current].size(); i++) {
x = (Integer) graph[current].get(i);
if (colour[x] != f && colour[x] != f + 1) {
if (colour[current] == f) {
colour[x] = f + 1;
} else if (colour[current] == f + 1) {
colour[x] = f;
}
queue[index++] = x;
} else if (colour[current] == colour[x]) {
return false;
}
}
}
}
}
return true;
}
public static void main(String[] args) throws IOException, Exception {
int second;
int one, two;
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(System.out)));
int test = nextInt();
int j, k, i;
for (i = 1; i <= test; i++) {
first = nextInt();
second = nextInt();
for (k = 0; k < first; k++)
graph[k] = new ArrayList();
for (j = 0; j < second; j++) {
one = nextInt();
two = nextInt();
graph[one - 1].add(two - 1);
graph[two - 1].add(one - 1);
}
out.println("Scenario #" + i + ":");
if (bfs( 2 * i) == false) {
out.println("Suspicious bugs found!");
} else {
out.println("No suspicious bugs found!");
}
}
out.flush();
}
}
You can not solve it using only bfs, this problem is about Bipartite graph
Just look it at wikipedia
I solved this problem more than a year ago. my profile
I am trying to implement Karger's min cut algorithm but I am unable to get the correct answer. Can someone please have a look at my code and help me figure out what I am doing wrong? Would really appreciate the help.
package a3;
import java.util.*;
import java.io.*;
public class Graph {
private ArrayList<Integer>[] adjList;
private int numOfVertices = 0;
private int numOfEdges = 0;
public Graph(String file) throws IOException {
FileInputStream wordsFile = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(wordsFile));
adjList = (ArrayList<Integer>[]) new ArrayList[201];
for (int i = 1; i < 201; i++) {
adjList[i] = new ArrayList<Integer>();
}
while (true) {
String s = br.readLine();
if (s == null)
break;
String[] tokens = s.split("\t");
int vertex = Integer.parseInt(tokens[0]);
this.numOfVertices++;
for (int i = 1; i < tokens.length; i++) {
Integer edge = Integer.parseInt(tokens[i]);
addEdge(vertex, edge);
this.numOfEdges++;
}
}
}
public void addEdge(int v, int w) {
adjList[v].add(w);
//this.numOfEdges++;
}
public ArrayList<Integer> getNeighbors(Integer v) {
return adjList[v];
}
public boolean hasEdge(Integer i, Integer j) {
return adjList[i].contains(j);
}
public boolean removeEdge(Integer i, Integer j) {
if (hasEdge(i, j)) {
adjList[i].remove(j);
adjList[j].remove(i);
this.numOfEdges -= 2;
return true;
}
return false;
}
public int getRandomVertex(){
Random rand = new Random();
return (rand.nextInt(this.getNumOfVertices()) + 1);
}
//Returns an array which consists of vertices connected by chosen edge
public int[] getRandomEdge(){
int arr[] = new int[2];
arr[0] = this.getRandomVertex();
while (adjList[arr[0]].size() == 0) {
arr[0] = this.getRandomVertex();
}
Random rand = new Random();
arr[1] = adjList[arr[0]].get(rand.nextInt(adjList[arr[0]].size()));
return arr;
}
//Algorithm for min cut
public int minCut() {
while (this.getNumOfVertices() > 2) {
int[] edge = this.getRandomEdge();
this.removeEdge(edge[0], edge[1]);
//Adding edges of second vertex to first vertex
for (Integer v: adjList[edge[1]]) {
if (!adjList[edge[0]].contains(v)) {
addEdge(edge[0], v);
}
}
//Removing edges of second vertex
for (Iterator<Integer> it = adjList[edge[1]].iterator(); it.hasNext();) {
Integer v = it.next();
it.remove();
this.numOfEdges--;
}
//Removing self-loops
for (Iterator<Integer> it = adjList[edge[0]].iterator(); it.hasNext();) {
Integer v = it.next();
if (v == edge[0])
it.remove();
//this.numOfEdges--;
}
this.numOfVertices--;
}
return this.numOfEdges;
}
public int getNumOfVertices() {
return this.numOfVertices;
}
public int getNumOfEdges() {
return (this.numOfEdges) / 2;
}
public String toString() {
String s = "";
for (int v = 1; v < 201; v++) {
s += v + ": ";
for (int e : adjList[v]) {
s += e + "-> ";
}
s += null + "\n";
//s += "\n";
}
return s;
}
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int min = 1000;
//Graph test = new Graph("C:\\Users\\UE\\Desktop\\kargerMinCut.txt");
for (int i = 0; i < 10; i++) {
Graph test = new Graph("C:\\Users\\UE\\Desktop\\kargerMinCut.txt");
int currMin = test.minCut();
min = Math.min( min, currMin );
}
System.out.println(min);
}
}