C :: Depth First Search Implementation

Mar 12, 2013

I'm having problems with implementing depth first search.

Code:
6 10 //6vertices 10edges
0 2 //vertex and its adjacent vertex
1 0
1 2
2 3
2 4
3 1
4 1
4 3
4 5
5 3

Output to terminal: 0 2 3 1 4 5
but it should be: 0 2 4 5 3 1

Here's my code:

#include<stdio.h>
#include<assert.h>
/* maxVertices represents maximum number of vertices that can be present in the graph. */
#define maxVertices 100
void Dfs(int graph[][maxVertices], int *size, int presentVertex,int *visited)

[Code] ....

View 1 Replies


ADVERTISEMENT

C :: Depth-first Search Of A Graph

May 18, 2013

You have to implement a data structure to represent graphs,directed or undirected,that tries to avoid the wasted space in the representation of a graph with adjacency matrix and the difficulty of searching the edges with adjacency list representation.We consider that the vertices are numbered from 1 to nverts and the exit degree of each vertex is at most MAXDEG. If deg[i] is the exit degree of the vertex i then the neighbors of the vertex i can be saved at the matrix edge[i][j], 1<=j<=deg[i].

Write a program that reads the datas from a file: if the graph is directed or undirected(1 or 0), the number of vertices (nverts),the number of edges (nedges) and the first and the last vertex of each edge.Write the function dfs that, with argument the data structure that you implemented before for the representation of a graph, prints the edges by the depth-first search of a graph. What I've done so far is: I wrote a program that reads these information from a file, calculates the exit degree of each vertex and creates the matrix edge[i][j]. What data structure do I have to implement???

View 1 Replies View Related

C++ ::  Depth First Search - Won't Compile

Jan 13, 2013

I've been trying to implement this Depth First Search graph algorithm using STL, and I get some error from Visual C++ 2012 at compilation.

The code is:

#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>

[Code].....

And the error says something like this:

Debug Assertion Failed!

Program: C:WindowsSYSTEM32MSVCP110D.dll
File: d:...vs2012vcincludedeque
Line: 1497

Expression: deque empty before pop

View 1 Replies View Related

C++ :: Depth And Breadth First Search Program

Mar 15, 2014

I am not sure how to have to user input numbers into a tree and to have it print out the steps it takes to reach the goal

Header File

#include <iostream>
#include <stack>
using namespace std;
bool recursive_depth(int);
bool is_goal(int);
void input_tree();
void print_tree();

[Code] .....

View 4 Replies View Related

C/C++ :: How To Implement Depth-First-Search With Adjacent Matrix

Nov 1, 2014

What I have done : Created functions to accept inputs from user and read,print out adjacent matrix.

What I need : How do I start DFS in adjacent matrix so that I can output the vertices of each connected component of a graph? I'm confused with adjacent matrix and the term 'component'.

#include <stdio.h>
#include <iostream>
#define maxV 8
using namespace std;
int V,E,x,y;
int a[maxV][maxV];

[Code] .....

View 7 Replies View Related

C++ :: Height Or Depth Of Binary Tree

Feb 23, 2014

I'm in interested to know what will be the height of binary tree with say 5 nodes (consider it balanced) is it 2 or 3?

9
/
5 13
/
3 7

and for following tree also:

2
/
7 5
/
2 6 9
/ /
5 11 4

is it 3 or 4?

i'm asking because, not able to find correct/one answer.

the algorithm i find is:

depth(struct tree*t) {
if ( t == NULL) return 0;
return max( depth(tree->left), depth(tree->right) ) + 1;
}

According to it, ans for second will be 4

But as per image on wikipedia [URL] .... the ans is 3

View 6 Replies View Related

C++ :: Program To Display N Depth Pyramid

Feb 21, 2013

Write a program that displays n depth * pyramid.

For example,Input: 4
Output:
*
***
*****
*******

This is what i have so far but it not giving me what i want.

#include <iostream>
using namespace std;
int main() {
int star;
int count=1;
cout<<"Enter the number: ";
cin>>star;

[Code] ....

View 1 Replies View Related

C++ :: Calculating depth Of Binary Tree

Apr 1, 2013

i have come across this code for calculating the depth of a binary tree.

int maxDepth(Node *&temp) {
if(temp == NULL)
return 0;
else {
int lchild = maxDepth(temp->left);
int rchild = maxDepth(temp->right);

[Code] ....

In these recursive calls i am really clue less about how the statements i numbered from 1 to 4 would be executed...in every recursive call...???

Lets temp has left depth of 3 and right depth of 100 then...in last 97 recursive calls max(temp->left) would be doing...????? how these recursive mechanism work here... I want to know specifically how these left node and right node recursive calls are co-ordinating with each other????

View 2 Replies View Related

C/C++ :: Applying Limit To Recursive Depth?

Jul 9, 2014

I wrote this code, but now need to apply a limit to the recursive depth. This is the format that I have to use. How would I pass a limit so that it stops after a given number? I'm just confused about where to apply it.

int compute_edit_distance(char *string1, char *string2, int i, int j, int limit) {
if (strlen(string1) == i) return strlen(string2) - j;
if (strlen(string2) == j) return strlen(string1) - i;
if (string1[i] == string2[j]) return compute_edit_distance(string1, string2, i + 1, j + 1, limit);

[Code] .....

View 4 Replies View Related

C++ :: Convert SUDOKU Solver From Depth-First To Breadth-First

Mar 5, 2013

Code:
/* Sudoku solver using dept-first search*/

#include <iostream>
#include <stack>
using namespace std;
struct valpos//structure which carries information about the position of the cell and its value {
int val;
int row;
int col;

[Code] ....

View 3 Replies View Related

C++ :: Search And Find The Shortest Queue And Search After Some Condition?

Mar 7, 2013

I am trying to implement a Task scheduler where i have n number of tasks. The Idea behind my task scheduler is that in a loop of queues of a vector, task should get enqueued to the shortest queue among the loop of queues, which is done by the following code.

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
task t // implemented in the other part of the program

[Code] ....

Next i am trying to extend this paradigm to reduce the overhead time of the scheduler, Instead of searching the shortest queue every time, search after some condition ie. search the shortest queue after 5 tasks gets enqueued to the shortest queue.

i need to do something like this

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
task t // implemented in the other part of the program
while(q[min_index].size()!=q[min_index].size()+5) // check whether current min_index queue's size is increased 5 more times if not goto enqueue

[code].....

View 1 Replies View Related

C++ :: Binary Search And Sequential Search Algorithm

Sep 16, 2013

Write a program to find the number of comparisons using the binary search and sequential search algorithms

//main.cpp
#include <cstdlib>
#include <iostream>
#include "orderedArrayListType.h"
using namespace std;
int main() {
cout << "." << endl;

[code]....

View 4 Replies View Related

C++ :: Binary Search Or Linear Search For 3D Array

Oct 7, 2014

inputting a search array. I tried putting a binary search but I can't get it to work. everything else works up until I put the value I am searching for in the array, then it just crashes.

How it suppose to work: input 2 coordinates with a value each then it calculates the distance between them then it suppose to let user search the coordinates for a value and state if found which coordinate it is at.

#include "stdafx.h"
#include <iostream>
#include <iomanip> //for setprecision
#include <math.h>

[Code].....

View 3 Replies View Related

C :: RSA Implementation And N Modulus

Aug 18, 2013

I have to develop minimalistic implementation of RSA algorithm in C for an embedded device.

I'm doing that for two days but I have run into a problem. The N modulus is the limitation for the maximum message value to be encrypted with RSA.

For example theoretically RSA-1024 can encrypt/decrypt messages 1024 bits long but I still cannot understand how to choose p and q values to produce N == pow(2, 1024).

Is it possible to encrypt/decrypt 1024 bits long messages in practice if the N < pow(2, 1024)?

So far I'm getting the following results

Code:
Encrypting with RSA
d=15689981, e=21, n=16484947
In=16484942, Encrypted= 6074492, Out=16484942 (OK)
In=16484943, Encrypted= 5468920, Out=16484943 (OK)

[Code] ....

View 10 Replies View Related

C++ :: Big Integer Implementation?

Aug 8, 2013

I was trying to implement Big Integer Implementation. I wanted to start with the basic operation of addition. I am having some problems with operator overloading part

/**
BigInteger implementation
*/
#include "conio.h"
#include "iostream"

[Code]....

View 9 Replies View Related

C# :: RSS Reader MVC Implementation

Apr 19, 2014

I was looking at this tutorial: [URL] ..... And I was wondering if implementing it in MVC would be pretty much the same way? How would I display feed items in the views page using?

I tried something like:

ReaderModel Reader = new ReaderModel();
Collection<Rss.Item> List;
List = Reader.GetFeed();
ViewData["RssItems"] = List;

// then in index.cshtml
@foreach(Collection<Rss.Item> items in ViewData["RssItems"]) {
<h3>items.Title</h3>
...
}

I don't think this is right as I'm getting those red error lines...

View 3 Replies View Related

C++ :: Operator New Without Implementation?

Aug 5, 2013

Here is the code,

Code:
class A {
private:
void* operator new(size_t size);
};
int main() {
return 0;
}

The code above compiles fine without errors. But operator new might not have implementation body?

View 3 Replies View Related

C++ :: Gravity Simulator Implementation

Jul 9, 2013

I've been working on creating a simulator to crash two galaxies together as part of a project to stress test a CUDA super computer. I've got a long way to go and am currently just working on correctly simulating n-body gravity functions. First I will use this to simulate the cores of the galaxies (the black holes) and eventually the stars.

So long story short I'm working on the beginnings of a gravity simulator. At this point I found some basic code that works well but doesn't quite give the effect I'm looking for.

The code below only pulls each object towards each other like a spring faster and faster until they shoot off into infinity. I try to give one of my bodies an initial velocity to get it to orbit another, but it always just shoots straight at the other body. I'm thinking I need to factor in inertia so that the initial velocity doesn't just get calculated away really fast by the other calculations.

I'm really looking for a bit of direction to get a real gravity simulator with orbits and such working right so eventually I can scale it up to a galaxy, throw in 100B stars and let the CUDA run for a month..

Code:
void update_galaxies(GLdouble elapsedTime) {
//Calculate gravity simulations
GLdouble r1, r2, r3;
r1 = r2 = r3 = 0.0;

for(unsigned int i = 0; i < galaxies.size(); i++)

[Code] ....

As you can see, I'm calculating all the bodies in a vector called "galaxies" with each other, and doing a basic gravity calculation to it. The update_position function simply takes the calculated acceleration and uses it to calculate the velocity and position based on the "elapsedTime".

I think I need to use the Varlet or Runge-Kutta integration methods, after doing a bit more research.

View 9 Replies View Related

C :: Stack Implementation Using Arrays

May 31, 2014

Code:

#include <stdio.h>#include <unistd.h>
#include <stdlib.h>
#define STACKSIZE 100
struct stackk
{
int top;
int items[STACKSIZE];
};
typedef struct stackk *s;

[Code]...

View 1 Replies View Related

C++ :: Stack Implementation With Struct

Apr 7, 2014

I am trying to implement a stack class which has struct data type as its private member. I am getiing the following error ,

bash-3.2$ g++ stack_str_arr.cpp
stack_str_arr.cpp: In member function ‘void stack::push(int)’:
stack_str_arr.cpp:39: error: ‘top’ was not declared in this scope
stack_str_arr.cpp: At global scope:
stack_str_arr.cpp:43: error: no ‘void stack::display()’ member function declared in class ‘stack’

Code:

#include<iostream>
using namespace std;
#define MAX 5
class stack {
public :
stack();

[Code] ....

View 2 Replies View Related

C++ :: 4x4 Matrix Inverse Implementation

Sep 19, 2013

I'm implementing a 4x4 matrix class and all is going well until the inverse function turned up. I'm trying to implement the inverse function, but I can't seem to get my head around it.

I've tried the internet, but found nothing useful. Also, I've looked into source code of other programs/libraries that implement a matrix class, but the code is unreadable.

How I can implement this damn 4x4 inverse function? I know the process of inversion, but putting that process into code is proving quite a challenge.

In addition, I do have some code, but it's unmanageable and inefficient at the moment. If you want to see it, just ask.

Additional question(s): What applications does the inverse matrix have in 3-D?

View 3 Replies View Related

C++ :: Error In MinHeap Implementation?

Nov 22, 2014

I'd like te have a heap which contains any item inserted by me. However, when I insert the values, if I delete the min value of this coded heap twice, I get the min value uncorrect.I could not find where the mistake is.

Code:

void BinaryHeap::percolateDown(int hole) {
int child = hole*2;
while (child < size) {

[Code].....

View 1 Replies View Related

C++ :: Implementation Of Named Class

Sep 12, 2014

I am trying to implement some kind of named class. It would look something like this:

class MyClass {
virtual std::string getName() = 0;
};

And now (what doesn't pass the compilation)

template <std::string NAME> class MyNamedClass {
std::string getName() { return NAME;}
};

And so every time I would like to have a class with a name, I could just do the following:

FinalClass : public MyNamedClass<"FinalClass">{};

The idea is not to have to always reimplement getName(), and just do it concisely in the declaration.

View 7 Replies View Related

C++ :: Hash Function Implementation

Aug 26, 2013

I was trying to implement a hash function in c++. This is just for learning purposes and not for a class project or assignment question. I had some questions before I started programming it:

1) Is it meaningful to have a hash function which maps string to string, string to int, int to int, float to int?
2) Can we have a template implementation which does element to element hashing?

I looked at several sources for a clear understanding of concepts and implementation technique but could not find a good source for reading about hashing.

View 3 Replies View Related

C++ :: Pathfinding Algorithm Implementation

May 11, 2013

I am currently trying to implement a pathfinding algorithm using c++ and opengl. My code so far is shown below:

#define OPEN 1
#define CLOSED 2
#define UNVISITED 3
#define BLOCKED 4
#define HIGHLIGHT 5
#define ONROUTE 6
#define GOAL 7

#define GSIZE 20 // size of tile grid
#define ISTART 15 // index position of starting tile in grid

[Code] ....

View 2 Replies View Related

C/C++ :: Implementation Of A Vector Of Structures

Apr 14, 2014

I created a structure Vector and implement some functions to make the new defined type (vector) dynamically allocated and resized (inspired from the C++ implementation of the dynamic arrays : vector). I need to assign a structure to every vector element but I am not sure that I am doing it right:

here is the structure that I've defined:

typedef struct  {
    void** mem; // to make this parametrizable I want the void* to point to a Structure (referenced with * a pointer)
    unsigned long     elems;
    unsigned long     elemsize; //element size

[Code] ....

I believe I have serious problems with pointers, values and dereferencing a pointer.

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved