C++ :: Graph Search Algorithm - Recursion Using Stacks

Mar 22, 2013

Any example of a graph search algorithm that uses a recursion and linked list based stacks to determine a route from a single point on a graph to another single point on a graph?

View 3 Replies


ADVERTISEMENT

C/C++ :: Recursion In Stack To Get Maximum Element And Copying Stacks

Nov 22, 2012

I am still new to stacks and recursion and am trying to implement two functions using recursion to get the maximum element in a stack and copying one stack into the other but it keeps repeating some elements and i dont understand why!!

this is the code for the functions:

void copyStack(MyStack &firstStack, MyStack &secondStack) {
    if(!firstStack.empty())    {
        string x=firstStack.top();
        firstStack.pop();
        //secondStack.push(x);

[Code] ....

View 2 Replies View Related

C# :: Algorithm With Recursion Use

May 12, 2014

To construct and write down algorithm of determination of the sum of squares of consecutive integers with recursion use. I tried to do something:

public static int RecSumSquare(int x, int n)
{
if (n < 0) throw new ArgumentException("n should be greater than zero");
if (n == 0) return 0;
else return x*x+RecSumSquare(x, n - 1);
}

But I don't know as the beginning and the end of this algorithm will look.

View 2 Replies View Related

C++ :: DFS Algorithm Malfunction - Graph Is Consistent

Apr 18, 2012

I wrote class DFS - Depth-first search. This algorithm check that this graph is consistent. Below is code of class

Code:
#ifndef DFS_H
#defineDFS_H
class DFS{
private:
std::stack<int, std::vector<int> > stos;//stos do przechowywania wezlow w dfs'ie
int *odwiedzony;
int liczbaWezlow;

[Code] ....

When i initialize dfs i use 2 parameters. On the next step i would like show all content array odwiedzony. Regardless of the given parameters of the array always has a value of zero. Where i made mistake and how to repair?

View 10 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 :: 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++ :: Boruvka's Algorithm - Find Minimum Spanning Tree Of A Graph

Apr 10, 2014

I am trying to implement Boruvka's algorithm to find the minimum spanning tree of a graph but my program crashes in the find function when i run it.

[URL] .....

sample input : [URL] .....

View 1 Replies View Related

C++ :: Dijkstra's Algorithm - Find Shortest Path In A Dense Graph

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

C++ :: Linked List - Recursion For Search Function

Mar 19, 2013

I have a linkedList search function given and I am having a little trouble getting it to work. I've made a few attempts with no success. Given normal search code:

template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const {
bool found = false;
nodeType<Type> *current; //pointer to traverse the list

current = first; //start the search at the first node

[Code] .....

My attempt to make it a recursive search:

template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const {
//bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first; //start the search at the first node

[Code] ....

View 3 Replies View Related

C/C++ :: Implement Breadth First Search Graph And Output Order Of Traversal

Mar 26, 2014

I'm new to program breadth first search and for my assignment we have to implement a breadth first search graph and output the order of the traversal. The problem is when I run my program I do not get any output from my traversal. All I get is "Breadth First Search Traversal starts at Vertex 2 and the order is: "

Here is my Queue class

//Declaration of node
struct node{
int info;
node *next;
};
//Defining Queue class
class Queue{

[Code] ....

View 4 Replies View Related

C :: Binary Search With String Algorithm

Oct 3, 2013

I'm trying to use the biSearch function to search for a keyword in the dictionary.

Code:
int biSearch(Dict DictEntries[MAXENTRIES],int start, int finish,char *keyword) {
int mid = (start+finish)/2;
int index = strcmp(DictEntries[mid].key,keyword);
printf("%s=%s
",DictEntries[mid].key,keyword);

[Code] ....

View 4 Replies View Related

C++ :: Binary Search Algorithm - Find Sum Of Used Number In Sequence

Mar 27, 2014

I have to made a programme which will search for given number and it must work in O(log(n)). The problem is that this programme beside finding this number have to find how many times this given number is used in this sequence.

Sequence is from lowest to highest only one possibility to use binary search algorithm

For example when we have squence
-1 -2 3 3 3 3 4 5 6 7 7 8 9 9 9 9 10

The numbers we need to search are
1 , 3 , 7 , 9 , 11 , 12

The answer is
0 , 4 , 2 , 4 , 0 , 0

So we need to find the sum of used number in sequence.

I have written algorithm Code: int start = 0;
int end = sequencelenght - 1;
int mid = 0;
/// Binary serach
while (start<=end) {
int mid=(start+end)/2;
if (sequence[mid]==givennumber) {

[Code] .....

As u see i search for given numer with binary with O(log(n)) but when i have to sum the duplicates the only good way i see is using loop to right and left but this have got log(n) specification (because when sequence would be for example 7 7 7 7 7 7 7 7 7 and given number to search will be 7 this will be O(n) loop).

How would look the most optimal algorithm look for this exercise? I mean O(log(n)) the fastest algorithm....

View 6 Replies View Related

C++ :: Array Of 20 Integers - Binary Search Algorithm To Locate Same Value

Nov 28, 2014

Write a program that has an array of at least 20 integers. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen.

/*
search benchmark.cpp
this program searchs through a array of 20 integers
*/

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int array [20];
int count;

[Code] .....

View 1 Replies View Related

C++ :: Write A Calculator Using Two Stacks

Apr 13, 2014

I just started by defining a stack class (stackDouble). I need to write a program that accepts an infix calculator expression, with the following operators (**, exponentiation, /, division, and, -, subtraction. The operator precedence is exponent, division, and subtraction.I need to use a stack of doubles and a stack of strings, of which I can write two classes, or write a single stack template. The user will input the expression just via cin, and there will be a # before every number, a ! before each operator, and a . at the end of the expression. '#', '!', or '.' can be input into a char variable, numbers into a double variable and operators into a string variable.

For example, the following would output 6:

# 3 ! / # 2 ! / # .5 ! ** # 2 .

As stated above, I already made up a stackDouble class. What would I need to do to create the other class (I don't think I want to do it with a template)?

Code:
#include <iostream>
#include <string>
using namespace std;
class stackDouble{

[code]....

View 2 Replies View Related

C++ :: How To Make A Template That Has 2 Stacks

Jan 11, 2015

How do I make a Template that has 2 stacks ?

Code:

template <class T>
class A {
};
template <class T>
class B {
};

is this code correct ?

View 1 Replies View Related

C++ :: How To Create A Deque Using 2 Stacks

Nov 27, 2013

I am trying to create a deque using these stacks but I failes again and again.

struct deque {
int key;
dek *next;
} *left = NULL, *right=NULL;
struct elem{

[Code] ......

View 5 Replies View Related

C/C++ :: Reverse String Using Stacks?

Oct 29, 2014

As part of my hw assignment i need to reverse a string using stacks. This is my attempt and so far its not working.

// reverse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

[Code].....

View 3 Replies View Related

C++ :: Data Structure - Program Using Stacks

Nov 3, 2013

Given a set of different types of paired symbols; determine whether it is balanced or not (the opening and closing version of each type are paired correctly). Any other type of characters may appear in the input will be neglected.

For example:
(**(*[*])****) is balanced, also [{}**[]**()]** is balanced. But ((*{*)*}) is unbalanced as well as [()***[]***{*(*)}.

Write a C++ program that requires a string containing and expression as an input from the user and check for balanced bracket pairs using stack data structure.

Here is my code which give me nothing when I run it.

#include <iostream>
#include <string>
using namespace std;
class CustomStack {

[Code] ....

View 1 Replies View Related

C++ :: Evaluating Postfix Expression Using Stacks

Nov 3, 2013

i feel like im really close to completing this but i cant seem to get the result printed out i dont think im calling my evaluate function correctly and its not performing the operations..

#include <iostream>
#include <stack> //stack header file
using namespace std;

[Code].....

View 1 Replies View Related

C++ :: How To Implement Main Queue Functions Using Two Stacks

Nov 8, 2014

How to implement the main queue functions Enque() and Deque() using two stacks S1 & S2. You allowed only to call push () and pop() functions of the two stacks to implement Enque()and Deque() functions.

#include<iostream>
using namespace std;
const int size=5;
int arr[size];
int front=-1,rear=-1;
bool isfull() {
if(rear==size-1)

[Code] .....

View 4 Replies View Related

C++ :: Convert From Infix Expression To Postfix Using Stacks?

May 24, 2014

I need to convert from an infix expression to postfix using stacks!

#ifndef EXPRESSIONMANAGER_H_
#define EXPRESSIONMANAGER_H_
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
using namespace std;
class ExpressionManager : public ExpressionManagerInterface{

[code]....

View 2 Replies View Related

C++ :: Recursive Boolean Function - Compare Two Stacks And Returns True If Identical

Oct 21, 2014

The question is to write a recursive boolean function that compares two stacks and returns true if they are identical. This is where I get stuck:

If the top items of both stacks are the same, the recursive call always returns true, because 'true' is saved on top of the return stack.

Here is my code:
template<class Type>
bool identicals(stackType<Type> s1, stackType<Type> s2) {
if(!s1.isEmptyStack() && !s2.isEmptyStack()) {
if(s1.top() != s2.top())

[Code] ....

View 2 Replies View Related

C/C++ :: Convert Infix To Postfix Using Linked Lists And Stacks - Program Stop Working After 1st Call

Sep 28, 2014

I was given a task to convert infix to post fix using both linked lists and stacks in the code so this is what i have written but the problem is it is giving me same error at three different places "missing function header(old style format?)

#include <iostream>
#include <string>
using namespace std;
const int size = 100;
class stack{
private: // Declare a structure for the list

[Code] ....

View 12 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 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++ :: Asterisk Bar Graph

Feb 12, 2014

i have to make a programs that prompts the user to enter quiz grades and add them up. For examples the user enters 6 test grades they are out of 5 so he enters 0-5 and i store them in the array. This part works great but now i have to print out a bar of vertical asterisks for every part too. So if at the end we have one test grades that are 2 grades of 1 points, 1 grade of two point, 2 grades of three point and 1 grade of 5 point it will have to display them as this

There are 2 grades of 1
There are 1 grades of 2
There are 2 grades of 3
There are 1 grades of 5

i need to do for loops but i am stuck on what to count too and what to print i know i will need cout << "*" and a couple of spaces.

#include <iostream>
using namespace std;
int main (){
int size;
int tests;
int a[6]={0};

cout << "How many quiz scores will you enter: ";
cin >> size;

[code]....

View 1 Replies View Related







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