Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,120 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,761 people online right now. Registration is fast and FREE... Join Now!




Sets/graphs in C++

 
Reply to this topicStart new topic

Sets/graphs in C++

madcivic
16 May, 2007 - 12:43 PM
Post #1

New D.I.C Head
*

Joined: 12 Mar, 2007
Posts: 2


My Contributions
Hi there.

im not that flash on all this programming but i have to do it for school and im into rewriting ECU's for cars so the basic information helps and i am slowley comming to grips with it all.

we have been given an assignment that states we have to write a graph using vectors.

we have to be able to add edges, vertexs and find a path through them.

could some one please show me some code to set up my classes or a good write up of how to do it because my lecture notes and what i can find on the internet still leave me in the dark. i have searched this site for SETS and GRAPHS but because these are words that are commonly used in english for other meanings they are hard to search for.

i realise giving the answers are frowned upon in ths forum but a step in the right direction would be muchly appreciated.

thanks Tom
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Sets/graphs In C++
17 May, 2007 - 11:51 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well you give a rather broad question. Exactly how you would want to represent a graph in memory really is up to you as there are LOTS of ways to do it. The basic way mimics how mathematics might store a graph in a matrix. There are however a few ways that mathematicians do that.

On way is to make each column and row represent a vertex. For a graph with 4 verticies this results in a 4x4 matrix, and therefore a 4 x 4 array. The element Array[row][col] tells you if there is an edge connecting those two. The nice thing about this is that you can represent directed graphs. For example if Array[1][2] = 1 but Array[2][1] = 0 then we know that there is a path from 1 to 2 but not a path from 2 to 1.

...1 2 3 4
...-------
1| 0 1 1 0
2| 1 0 0 1
3| 1 0 0 0
4| 0 1 0 0



This method has its advantages, it also has its disadvantages. It wastes space for many types of graphs, it is not all that easy to add in verticies (you have to create a new array, and then copy over that data.) BUT we can also represent directed graphs, weighted graphs, colored graphs etc etc etc using this method.

Another method of representing a graph is an array of 2D values. X and Y would be verticies and the pair represents an edge. This again has its ups and its downs. For example listing all of the verticies requires you to search the entire array looking for them. This is why there generally is also another array containing the possible verticies. (This seems to be what you teacher is asking for....)

Another method of storing graphs is to use a graph data structure (but your teacher seems to want to you use vectors). This is much like a multi-linked linked list.

If your requirements are just to add/delete vertecies, add/edit/delete edges, and find paths that either of the two examples I gave will work. The second one might be easier to impliment.
User is offlineProfile CardPM
+Quote Post

madcivic
RE: Sets/graphs In C++
17 May, 2007 - 12:22 PM
Post #3

New D.I.C Head
*

Joined: 12 Mar, 2007
Posts: 2


My Contributions
thanks for the reply, and with your help and another day at it i am making some progress...i think,

again thanks although i will, no doubt be calling on the forum again.

Tom
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:03PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month