0 down vote favorite
I have a list of, or we can say a chain of, items in a database table, which are connected with each other like:
> item_id connected_item item_cost item_type
> 100 200 12 anything
> 200 300 11 anything
> 300 400 14 anything
> 400 500 19 anything
⦠so on
How can I know that if I input 200
it will give me both connections to it, which are 100 and 300. And also, the list of connected items between two items e.g. if first item=100
, last item=300
then all items are 100,200,300
. And the approach should also work bottom to top e.g. if first item=300,
last item=100
then all items are 300,200,100
including cost and type.
It is a SQLite database, there is no problem in loading data I loaded the given data in a NSArray
from ItemsDatabase
class which is Database manager class.And a object class for this table name 'Items'
which has properties having above column names just like the tutorial https://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app. Now i am just looking for logic to get the solution of my above given problem.
If any further elaboration needed let me know.
Let me define myself more what i am trying to do here
STAND_ID_FK CONNECTED_STAND DISTANCE_BETWEEN_STAND TIME_BETWEEN_STAND
101 102 1 2
102 103 0.8 1.5
103 104 1.4 2
104 105 1.007 2
105 106 1.264 2
106 107 0.8 1.5
this is the database table which is actually a bidirectional graph each bus stand is a vertex . So from here i have to do following things
- Get all connected stands with any stand.
- Get total time and distance between two stands.
- Get a list of stands which contains stands between two stands Foe eg. if source=101 and destination = 105 list of path contains 101,102,103,104,105.
So i have to create an adjacency list and traverse it when user input any source and destination. Please look at it I am fed up solving the logic.