DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Need to know how to write C++ code to draw a graph with adjacnecy list

#### Hybrid View

1. Registered User
Join Date
Sep 2006
Posts
17

## Need to know how to write C++ code to draw a graph with adjacnecy list

Hi all;
I want to write a program for my own practice . In this program, I Need to know how to write C++ code to draw a graph with adjacnecy list. For thsi I prepared an input file with data in array type and also wrote the code to read input files. Now I need help to write C++ code to draw a graph with adjacnecy list Here is the more details of my problem.

My Input: A directed graph G represented by a set of vertices and a set of edges.
My Output should : Two parts. The first part is the same graph G represented by adjacency lists. The second part is the transpose GT represented by adjacency lists
thanks

2. Registered User
Join Date
Jan 2005
Location
UK
Posts
604
Hi,
are you talking about a graphical representation of your graph? Painting on your screen depends entirely on the O/S and compiler you use. We need more info to help you there...
Cheers,
D

3. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
I think that linked lists of the vertices and edges are more direct to deal with rather than arrays. You are going to have variable number of edges which are adjacent to a vertex.

Create a vertex struct and an edge struct. This is the place to store a name or other identifier of the graph vertex, other information about that vertex, the distance or weight of the edge, and links/pointers to the next struct in the list.

Your Vertex structs in your vertices list will have links to the next vertex in the vertices list and a link to the first edge in the edge list. Add edge structs as you encounter them. If this is a undirected graph you need to make two entries for each edge.

Your approach to the transposed graph should be self evident when you have your other structure. (copy the vertice list, reverse the direction of the edge and redistribute the edges in the edge lists)

There are LOTS of resources online or in your local (college/university) library which might help you with the code for this project. Google "C++ adjacency list" and you should find some help with the actual code (this is a nice programming project for college data structures classes ...)
Last edited by nspils; 09-18-2006 at 09:19 AM.

4. Registered User
Join Date
Sep 2006
Posts
17
TO Dkyb: yes, I am talking about a graphical representation of graph. Please provide any c++ codes if you have it and I want to try plug them in my codes and test it.
Thanks for your time and input.

TO nspils:
Thanks for the details answer, is anyway I can use array type input data and able to draw the graph? If possible then can you provide some samples of C++ code to draw the graph, please?

5. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
My response was addressed to the "data structure" for addressing/manipulating the graph, not its presentation to the screen (drawing it). My mistake - I misread what you were asking.

6. Registered User
Join Date
Jan 2005
Location
UK
Posts
604
Hi,
I can only tell you 'bout VS:
Everytime you draw on a device (eg screen, printer, plotter,...) you net to attain a pointer to the devices "device context" and use the methods in the DC to do the painting.
Code:
```// use this code in the OnDraw - event - handler in your View-class or Dialog - class
CDC* pDC = GetDC();
pDC->MoveTo(x1,y1);
pDC->DrawTo(x2,y2);
pDC->SetPixel(x3,y3,RGB(127,255,12));
pDC->Rectangle(x1,y1,x2,y2);
...```
You'll have to read up in the MSDN on the exact syntax (and pitfalls), but in general that's it.

Cheers,
D

7. Registered User
Join Date
Sep 2006
Posts
17
Hi all;
thanks for the valuable input. Let me try and if i need more help I will reply you.

thanks
shailesh33

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source

Top DevX Stories