How to randomly access a point in a CvSeq? - c

Can we randomly access a point in a CvSeq object? We can traverse it, so I imagine it's possible in a simple manner. How is this accomplished?

I have found it. There is a method called cvGetSeqElem, which takes in the sequence and the index. Thanks for the help though. This might just follow the linked list linearly, but it's simpler than manually coding the search.

Looking at the OpenCV API (http://opencv.willowgarage.com/documentation/dynamic_structures.html) it doesn't sound possible. Looks to be some form of linked list implementation, which means that the only way to access an element part way though is to follow the links.

cvSeq is a linked list - you have to follow the chain of links, you have no idea where the next entry is stored in memory.

Related

Loop detection in connected graph

Most example I found only treat single linked lists. I need a solution for a multiple linked list.
Image is easier (valid):
Invalid:
Which algorithm would be able to return the begining of the loop (B) and not collide with E? A good starting point would be also to know if there is a loop at all.
Stuff like this or edge counting doesn't work (because not single linked...).
Thanks.
Just check if a route from 'end of connection node (B)' to 'start of connection node (C)' exists, if yes, a new loop would be created. Doesn't quite answer it, but good enough...

Solve maze using queue data structure?

I'm taking a class in data structures and was given the assignment to find the shortest path through a maze using C and implementing the queue data structure. However, I can't really wrap my head around how to use a queue here.
I know the idea is to count every possible move from the start position, and when you hit the target, you're supposed to trace back to the initial position. This is what I don't understand. Because if I use a queue and delete all the moves that leads up to the target, I have no data to use to do the trace back, and if I don't delete the moves that lead to the target (i.e. saving all the possible moves and deleting them when I actually do the trace back), I might as well be using a stack.
I know there's something I don't quite get, but I can't figure out what it is. How would I utilize the queue data structure in this case?
What your professor is trying to get you to use is called "breadth-first search". The queue comes in for deciding which spaces to explore next. When you are looking at the possible paths to take, you enqueue all the paths you have yet to explore. Instead of continuing down the path you're on (which would be "depth-first search"), you dequeue the next spot you need to check, which will take you back to one of the positions you were considering earlier.
The actual implementation is up to you, I'd recommend looking for examples of breadth-first search online.

Implementing Intelligent design sort

This might be frivolous question, so please have understanding for my poor soul.
After reading this article about Intelligent Design sort (http://www.dangermouse.net/esoteric/intelligentdesignsort.html) which is in no way made to be serious in any way, I started wondering whether this could be possible.
An excerpt from article says:
The probability of the original input list being in the exact order it's in is 1/(n!). There is such a small likelihood of this that it's clearly absurd to say that this happened by chance, so it must have been consciously put in that order by an intelligent Sorter.
Let's for a second forget about intelligent Sorter, and think about possibility that random occurrences of members in array are in some way sorted. Our algorithm should determine the pattern without changing array's structure.
Is there any way to do this? Speed is not a requirement.
The implementation is very easy actually. The entire point of the article is that you don't actually sort anything. In other words, a correct implementation is a simple NOP. As my preferred language is Java, I'll show a simple in-place implementation in Java as a lambda function:
list->{}
Funny article, I had a good laugh.
If the only thing you're interested in is that whether your List is sorted, then you could simply keep an internal sorted flag (defaulted to true for an empty list) and override your add() method to check if the element you're adding fits the ordering of the List - that is, compare it to the adjacent elements and setting the sorted flag appropriately.

How to implement OPT Page Replacement algorithm in C?

Hello I am trying to implement the OPT Page Replacement algorithm:
Currenly I have created a linked list for all the future memory access references.
And my initial idea was to compare each reference in my linked list and mark down the distance for its next appearence as an attribute. When actually running the program and a page fault happens, I will look through every page in my page table and evict the page that has the longest distance.
However, I find my idea quite complicated and inefficient to implement. Is there a simplier way to implement this algorithm? Thanks.
The swaps made are the same for these two executions: (1) OPT on the original sequence of requests (2) LRU on the sequence of requests in reverse order. You can implement LRU via the doubly-linked-list strategy outlined in the linked Wikipedia article.

Best way to take input for a graph Data Structure in C?

I am working on a basic graph implementation(Adj List based) in C so that I can re-use the basic structure to solve all graph related problems.
To map a graph I draw on a paper,I want the best and easiest way.
Talking of the way I take the input rather then how should I go about implementing it! :)
Should I make an input routine which asks for all the nodes label first and then asks for what all edges are to be connected based on two labels?
What could be a good and quick way out? I want an easy way out which lets me spend less amount of energy on the "Input".
Best is to go for input of an edge list,
that is triplets of,
Source, Destination, Cost
This routine can be used to fill Adj List and Adj Matrix.
With the latter, you would need to properly initialize the Matrix though and setup a convention to determine non existent edges.
Here you find details about representation of graph:
Graph-internal-representaion
However here some codes in c++ and java are also given,which you can easily convert to C codes.

Resources