Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

New User to Graph::Directed Seeks Help!!

by arunhorne (Pilgrim)
on Jul 19, 2002 at 04:44 UTC ( [id://183167]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

arunhorne has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm having a few teething problems with the Graph::Directed module and was hoping someone could put me straight.

  1. First up I want to label the edges in my directed graph, but not with a weight but just a textual label (i.e. E1--A-->E1 such that vertices E1 and E2 are connected by the edge A). Thus when I come to do a search through the graph for the shortest path between 2 nodes I can see which edges were travelled along. (Will this even be possible with Graph::Directed?)

  2. My second problem is finding out how many vertices/edges are in my graph for verification purposes. The documentation notes I can use $G->has_vertices(@v) in scalar context, this is fine but what is the @V for??

I've posted my code below... it works fine but I need to add the features I described above. Vertices are created implicitly by the add_edge method. Don't worry about the duplicate self-referencing edges (what a mouthful!) - once each has been given an individual label, for example, E3 will be self referenced by three distinct edges (rather than the three anonymous and thus identical edges created by my code) - this is a feature of the problem domain I am handling and not something I have control over. Code below:

use Graph::Directed; use warnings; use strict; # Instantiate my $G = new Graph::Directed; my @V = (); # Create graph $G = $G->add_edge("E1", "E2"); $G = $G->add_edge("E3", "E3"); $G = $G->add_edge("E3", "E2"); $G = $G->add_edge("E3", "E1"); $G = $G->add_edge("E2", "E2"); $G = $G->add_edge("E2", "E2"); $G = $G->add_edge("E2", "E2"); $G = $G->add_edge("E3", "E3"); $G = $G->add_edge("E3", "E3"); # Print some info about graph print $G, "\n";

Looking forward to your words of wisdom, best wishes,

____________
Arun

Replies are listed 'Best First'.
Re: New User to Graph::Directed Seeks Help!!
by amphiplex (Monk) on Jul 19, 2002 at 05:05 UTC
    Maybe this helps:
    print "number of vertices: ".$G->vertices."\n"; print "number of edges: ".$G->edges."\n"; print "vertices: ".join (':',$G->vertices)."\n"; print "edges: ".join (':',$G->edges)."\n"; # set attribute name to "A" for edge E1 -> E2 $G->set_attribute("name","E1","E2","A") or die "error setting attribut +e to edge: $!\n"; print "Name of edge E1->E2 is ".$G->get_attribute("name","E1","E2")."\ +n"; print "Name of edge E2->E3 is ". (defined $G->get_attribute("name","E2","E3") ? $G->get_attribute +("name","E2","E3") : "<undef>") ."\n";

    update: I think you missread the docs concerning has_vertices:
    @V = $G->vertices In list context returns the vertices @V of the graph $G. In scalar context returns the number of the vertices. $G->has_vertices(@v) In list context returns a list which contains the vertex of the vertices @v if the vertex exists in the graph $G and undef if it doesn't. In scalar context returns the number of the existing vertices.

    ---- amphiplex

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183167]
Approved by Corion
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.