C++ :: Dijkstra Algorithm - Adjacency Lists
Feb 28, 2014
So I have been trying to implement the Dijkstra Algorithm for shortest path in a directed graph using adjacency lists, but for I don't know what reason, it doesn't print out the results (prints the minimum distance as 0 to all nodes).
The code I wrote is:
#include <fstream>
#include <functional>
#include <climits>
#include <vector>
#include <queue>
#include <list>
using namespace std;
struct node {
int vertex;
int weight;
[Code] ....
The input data was:
5 7
1 2 10
1 3 2
1 5 100
2 4 3
3 2 5
4 3 15
4 5 5
It means there are 5 nodes, 7 arcs (directed edges), and the arcs exist from node 1 to 2 with the cost of 10, from 1 to 3 with the cost of 2, and so on.
However, the output is wrong. Where the program might fail. I took the main idea from here: [URL] ....
(At the end it gives the idea for Dijkstra's Algorithm using a priority_queue).
View 3 Replies
ADVERTISEMENT
Jan 30, 2015
I'm attempting to build a tool for a Minecraft mod called Thaumcraft. In it, there are various aspects of magic that are used in a little research minigame; basically, you have to link Aspect A to Aspect B by the aspects either used to make them, or the aspects that use them in their creation. I figured the easiest way to find a path from A to B would be to relate them via graph object, the code for which I found here, minus the IEnumerable<T> dependency on the Graph<T> class itself, because that requires I build an IEnumerator<T> class and it seems difficult I can do without.
Must be between MinSteps and MaxSteps (say, between 3 and 5 steps).Must use either a source aspect (as in, the current aspect requires it to be built), or a destination aspect (as in, the current aspect is used in its creation).An aspect requires two other aspects of a lower tier to create it.The only exceptions to the above rule are the Primal aspects, which require no aspects to build them.
Here's my Aspect class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Pathfinder
[code].....
View 2 Replies
View Related
Mar 20, 2014
Program to implement Dijkstra's Algorithm. Have Problems storing graph info into linked-list . The input is in the following format
5
1 2 9.0
1 3 12.0
2 4 18.0
2 3 6.0
2 5 20.0
3 5 15.0
0
1 5
The first number is the number of vertexes in the graph. Then next lines up to 0 are the edges of the graph. With the first and second numbers being the vertexes and the third being how far the edge is between them. Trying to read in the data and store the edges into there locations in the List adjacency for that vertex. This example would make a graph with five vertexes with edges from 1 to 2&3. 2 to 4&3&1 etc. It also stores in the opposites ex 2 1 9.0.
When reading it in and printing it back out it seems that it is only storing the last data entered for that vertex. EX. When trying to print out the data read in i only get 1 3 12.0, 2 5 20.0, 3 5 15.0, ( 4 2 18.0, 5 3 15.0 would show up if `if(i<n)` was removed it is there so it does not show duplicate edges).
#include <cstdio>
using namespace std;
struct ListCell {
ListCell* next;
int vertex;
double weight;
ListCell(int v, double w, ListCell* nxt)
[Code] ....
View 5 Replies
View Related
Apr 19, 2012
Code that finds a shortest path in a dense graph, using Dijkstra's algorithm in the simple array version. You write a function
struct listnode shortest path(int n, int s, int t, int *dist)
with
struct listnode f struct listnode * next; int vertexnumber; g ;
Being used to return the list of vertices on the shortest path. Your function has the following arguments:
- n: the number of vertices of the graph,
- s: the start vertex,
- t: the target vertex
- dist: the matrix of edgelengths.
The vertices are numbered from 0 to n -1, so s and t are numbers in that range. dist is a pointer to the n * n matrix of edgelengths; vertices which are not connected will be joined by an edge of length 9999. To access the array element dist[i][j], we can use *(dist + i*n + j). Your function should return the list of vertices on the shortest path from s to t, starting with s and ending with t.
View 4 Replies
View Related
Feb 24, 2013
I've written code for a selection sort algorithm to sort through a singly linked list, However, it only brings the smallest node to the front, but doesnt swap positions of any of the remaining nodes, even though they are not in order.
Input list: 3 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 0 6 12 16
Sorted list: 0 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 3 6 12 16
Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int val;
struct node *next;
[Code] ......
View 1 Replies
View Related
Mar 1, 2014
Today I am refining my skills on graph theory and data structures. I decided to do a small project in C++ because it's been a while since I've worked in C++. I want to make an adjacency list for a directed graph. In other words, something which looks like: 0-->1-->3 1-->2 2-->4 3--> 4-->This would be a directed graph with V0 (vertex 0) having an edge to V1 and V3, V1 having an edge to V2, and V2 having an edge to V4, like this:
V0----->V1---->V2---->V4
|
|
v
V3
I know that in order to do this, I will need to create an adjacency list in C++. An adjacency list is basically an array of linked lists. Okay, let's see some pseudo C++ code:
Code:
#include <stdio>
#include <iostream>
using namespace std;
struct graph{
[Code] ....
This pseudocode is currently far from the mark. And that is what -- pointers and structs in C++ have never been my strong suit. First of all, this takes care of the vertices that a vertex points to -- but what about the vertex itself? How can I keep track of that vertex? When I loop over the array, it will do me no good to only know what vertices are being pointed to, rather than also knowing what points to them. The first element in each list should probably be that vertex, and then the elements after that are the vertices it points to.
But then, how can I access this first element of the list in my main program?. I would like to be able to loop over this adjacency list to do some cool things with graphs. For example, to implement some graph theory algorithms (sorts, shortest paths, etc) using the adjacency list representation. (Also, I had a question about the adjacency list. What is different than just using a list of arrays? Why can't I just have a list with an array at each element in the list?)
View 2 Replies
View Related
May 3, 2014
"My Programm is crashing and i dont know why?"
#include<iostream>
using namespace std;
int arr[3];
struct edges {
int edge_data;
edges *next;
[code]....
View 1 Replies
View Related
Nov 7, 2013
I got a permutation question, I got two different std::list:
list<string> slist;
slist.push_back("str111");
slist.push_back("str222");
slist.push_back("str333");
list<int> ilist;
ilist.push_back(100);
ilist.push_back(200);
I need the permutation for both two lists, the result should be like this:
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
template <typename value_t>
void dump_list(const list<value_t>& lst) {
[Code] ....
See, there are two do while loop, if I need a permutation with more than two lists, there'll be more and more do-while loops, that's make code looks ugly, I wonder if stl has some tricky way that can do this with just one next_permutation.
View 5 Replies
View Related
Dec 27, 2013
I'm trying to set up a simple implementation of a double linked list. I can't make it fly.
Code:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
[Code] ...
I seem to create a root vertex, but I can't establish if I connect sub nodes to my list.
View 13 Replies
View Related
Mar 6, 2015
I am merging two linked list but the output of the program is not printing insert
Code:
typedef struct Merge
{
int info;
[Code].....
View 1 Replies
View Related
Oct 14, 2013
In our homework assignment it states to, "write a set of dynamic linked lists" and so on. Now, the problem I'm confusing myself is at the storage inside of each node. To my understanding, each node contains the prev and next nodes, but also a pointer to the data, in this case being a string. He has asked us to manage these strings as rows of chars such as
char[0] = c // first node being addressed here
char[1] = a
char[2] = t
char[3] =
char[4] = d // second node starting here
char[5] = o
char[6] = g
char[7] =
I have written my code where each node is holding a string, not separated as shown above... my question is to how you can build your doubly linked list where each node is being address a set of chars.
View 1 Replies
View Related
Apr 5, 2013
New to this C stuff, and was going through Kochan's book Programming in C and got to chapter 9 structured lists.There's this problem that requires you to set a Variable N that calculates days.
N = 1461 x f(year, month) / 4 + 153 x g(month) / 5 + day
where
f(year, month) = year-1 (if month <=2)
=year (otherwise)
g(month) = month+13 (if month <=2)
= month+1 (otherwise) Code:
struct date {
}
[code]....
I was having issues getting the right values for N so I tried to see what T1.year gives me when I type in firstDay.month=02, firstDay.day=08 and firstDay.year=1999, and I got 8. According to book I should be getting 1998.
View 2 Replies
View Related
Feb 26, 2015
I created a bunch of nodes and I made each one before one another. Im having trouble adding another node to the last one.
#include <iostream>
using namespace std;
struct nodeType{
int info;
nodeType *link;
};
void printList(nodeType *head) {
nodeType *current = head;
[code]....
The node with the value of 400 is the node that has to be last.. How would that work?
View 3 Replies
View Related
May 11, 2014
I'm trying to do is let the user type in something like A654321 (note: these are ALL the characters, i.e. 'A', '6', '5', '4', etc..) Then I just want to display that current number back to them and I am getting some weird pointer memory allocation error..
#include<iostream>
using namespace std;
//To rename data-type "int" or integer to "element" (Both are equivalent)..
typedef int element;
//To declare an appropriate SENTINEL.
char const SENTINEL = '#';
[code]....
It seems like the first part that it bugs out at is in my Clean(); function..
Unhandled exception at 0x01383FBB in VegMe.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.
It doesn't like head = head->next; ?Also, ignore the ReverseList(); function, it is not being used as of now.
View 7 Replies
View Related
Apr 16, 2015
I want to get ride off lists and vectors in this program i also don't want to use classes, just want to use strings functions and structures...
#include <iostream>
#include <string>
#include <list>
using namespace std;
struct contact {
string name;
int studentiD;
[Code] ....
View 1 Replies
View Related
Aug 12, 2014
What I want to create is a program that sorts through a huge list (millions of lines).
I want it to get rid of any line that contains a word that isn't in an English dictionary.
Example list:
00sdfdsf
ahdadsg
angel
ksjflsjdf
green
green000
carrot
and it would go through millions like that, giving me only:
angel
green
carrot
as my new list.
How could I go about this? What programs would I need? And at the very least how can I remove unwanted things like numbers, double letters, underscores etc.?
View 3 Replies
View Related
Nov 13, 2013
I am working on building a graph of a maze problem using an adjacency list and my own custom class Node.
Each Node has a vNum and jumpAmount integer variable, the vNum is the unique key.
If I have an array of lists of Node objects declared by: Code: forward_list<Node> adjacencyList[10] I skip the 0 row and only use 1-9. It makes more sense logically for my program and Im not worried about wasted memory at this point.
How could I go about traversing and outputting member variables for all the Nodes in each list in my array?
Say my code builds the following adjacency list:
Code:
1 -> { 2, 3, 4, 7 }
2 -> { 1, 3, 5 }
3 -> { 1, 2, 6 }
4 -> { 1, 5, 6, 7 }
5 -> { 2, 4, 8 }
6 -> { 3, 4 }
7 -> { 1, 4, 8 }
8 -> { 5, 7, 9}
9 -> { 8 }
where the values in brackets are the nodes that have an edge with the ith row node. Had to put it in code tags for the thread to be allowed to post.
I am using the forward_list library for my list functions and this is where my problem arises. The cplusplus.com - The C++ Resources Network website iterates through the list using:
Code:
for ( auto it = mylist.begin(); it != mylist.end(); ++it )
std::cout << ' ' << *it;
Obviously this works for ints, doubles, etc, but not objects with member variables. My attempt to replicate this code for my problem looks like:
Code:
for( int i = 1; i < 10; ++i ) {
cout << "Node " << i << " -> { ";
for( Vertex it = adjList[i].begin(); it != adjList[i].end(); ++it )
{
cout << it.getVNum() << ", ";
}}
I have to come up with user defined conversions for this to work and I have never done such a thing. Is there a way to go about doing this to avoid user defined conversions, because if there isnt I feel like attempting to do this specific problem might be a little too difficult for someone who hasnt defined any custom conversions.
Potentially, I could write my own List class and have a next pointer member variable that points to the next object in the list and use that to use a dot reference to a get function to display member variables. This is how I learned linked lists back in my Data Structures class. However I am trying to avoid that for the time being, as that would be a lot more code to implement rather than just figuring out how to use the forward_list library.
View 4 Replies
View Related
Sep 21, 2013
I am writing a Windows Form program, and currently it has 3 Lists. I need to loop through all three, and use each possible combination, so am using three nested for loops.
I found that if the outer loop had less elements than one of the inner loops/lists, some were missed off, as I need to save all the possible combinations of list a + b + c.
This is what I wrote:
Code:
public int CalculateOrderToProcessLists()//for three lists
{
int order = 0;
if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() > sListThree.Count())
{
order = 123;
[Code ....
My problem is I would like to be able to have 4 (or more if possible) lists, and to calculate what order to process the lists will be exponentially more complex.
Any better way of comparing the sizes of the lists, so that I can use the nested loops of say 4 lists, but being able to use N number of lists would be awesome.
View 2 Replies
View Related
Aug 8, 2013
I am having some trouble getting a 3d array of pointers to link up with a linked list for each of the array elements. the 3d array of pointers is declared like this:
Code:
struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS]; and there are linked lists declared like this:
Code: struct particle { /* structure for particles */
double sw[3]; /* square well components */
double hs[3]; /* hard sphere components */
double u[3]; /* unit vector for rotations */
struct particle *link;
};
I want to get the array pointers 'cell[x][y][z]' to point to the first observed particle in a function next to main, something like this:
Code:
void generate_list(){
int i,j,k,l;
/* determine the number of cells to decompose the simulation box in each of x, y and z directions*/
int(cells_x) = floor(boxX/cell_size);
int(cells_y) = floor(boxY/cell_size);
int(cells_z) = floor(boxZ/cell_size);
/* initialise the array of pointers to NULL */
for (j=0;j<cells_x;j++){
[Code]...
I am getting a pointer type cast error when I compile "assignment from incompatible pointer type",
View 6 Replies
View Related
Jun 20, 2013
I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:
78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc
Each company data will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in a linked list. Each node structure will contain a pointer to one company data, and a pointer to the next node structure.This is all I have so far..
Code:
typedef struct {
char *companyName;
double hourlyPrices[NUM_PRICES];
double avgPrice;
} COMPANYDATA;
}
[code]...
We have to sort the nodes alphabetically. My question is, how do I read the data from the file one line at a time, and insert them alphabetically one by one?
View 7 Replies
View Related
May 2, 2013
I'm trying to compare a single value with a value from my Linked list and if they are same, I want to add the value from the list to new list. In other words I want to create a new List with values with the first one. Here is the code that I made, but it's not working.
This is the code with which I search in the first list for a node with a value. In the main() function I have A.find_city(), so it can start from the start_pointer from the first list:
void List::find_city() {
List *temp1;
int b = 0;
char city[20];
cout << "Enter city: ";
cin >> city;
temp1 = start_ptr;
[Code] ....
This is the code with which I add a node to the new list:
void List::in(List *temp1) {
List *temp2;
if(start_ptr == NULL)
start_ptr = temp1;
[Code] ....
View 9 Replies
View Related
Jun 7, 2014
I have this code that I need to memorize for my final. Memorizing code is easy for me, but I'm trying pretty hard to fundamentally understand the functions, and what they are doing (even using pen and paper, to draw and trace).For example, in the push function below, I understand everything, except why I'm setting ptr = p. I feel like p should be equal to NULL, then the next node I push should be equal to p, etc.
Stack & Stack::push(double x)
{
Node * p = NULL;
try
{
p = new Node;
}
[code].....
Also, are LL Queues that hard to implement once you can do them w/stacks - That will probably be something I have to code for my final, as well. Below is the full code for my Stack class.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Stack
}
[code]....
View 1 Replies
View Related
May 1, 2013
I have a problem like this: I do not know how to count the number of elements in a list of lists. I have the following list of list.
#define VAR(V, init) __typeof(init) V=(init)
#define FOR_EACH(I,C) for(VAR(I, (C).begin()), ite = (C).end();
(I) != ite;
++(I))
std::vector<std::vector<GLdouble> > contours;
[Code] ....
I do not know how to write the part indicated in Bold that represents the number of elements of contour. As seen from the code used for the tesselation OpenGL.
View 3 Replies
View Related
Feb 10, 2014
A static method named readFromFile that takes a C-string as the first parameter, and an orderedLinkedList<MemberDO> object as the second parameter. The first argument is the filename that contains data for existing members. This method should read the data for each individual member from the input file (one line of data per member), create a new MemberDO object, and insert this object into the linked list specified in the second argument.
How do I take the second parameter in, do I need to create the Linked List first? Here is the code I have so far
#include<iostream>
#include<string>
#include <fstream>
using namespace std;
class MemberDO {
[Code] ....
What to do with the readFromFile static method?
View 1 Replies
View Related
Jan 31, 2014
Can we compare vectors with linked lists?
View 4 Replies
View Related
Feb 19, 2013
i write code that's merge two linked list without sort ....
node * merage (node * list1, node * list2) {
// check witch list is empty
if (list1 == NULL) return list2;
if (list2 == NULL) return list1;
if ( list1 == NULL && list2 == NULL ) return NULL;
[Code].....
View 1 Replies
View Related