Tuesday, April 10, 2012

How to print a boost graph in graphviz with one of the properties displayed?

I see examples of this when using property maps, but not when using structs to handle the vertices and edges (I think this is called 'bundles').


I have vertices and edges defined as such, in an adjacency list graph.


struct Vertex
 {
     string name;
     int some_int;
 };
 
 struct Edge
 {
     double weight;
 };
 


The graph is constructed as follows:


typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, Vertex, Edge> boost_graph;
 


I want to print my graph of these objects in Graphviz format, so I can view it as an image. However, I don't only want the nodes and edges. I also want the attribute name on vertices and weight on edges to appear in the image. How can I do this?


Answer:

I gave bad info the first time. Here is the correct answer.
#include <boost/graph/graphviz.hpp>
using namespace boost;
// Graph type
typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperty> Graph;
Graph g;
std::vector<std::string> NameVec; // for dot file names

// write the dot file
std::ofstream dotfile (strDotFile.c_str ());
write_graphviz (dotfile, g, make_label_writer(&NameVec[0]));

tsql conditions vs c# conditions performance

I would like to know what performs faster and its preferable a condition in a tsql query like this:



select case 'color' when 'red' then 1 when 'blue' then 2 else 3 end



or performing the same switch in c# code after getting the value from the db?



switch(color):
{
case "red":
return 1;
case "blue":
return 2;
default:
return 3;
}


Thanks beforehand





Insert into Multiple Tables using PHP with LAST_INSERT_ID()

Below is the code that I've tried but I can't seem to make the right tweaks to get the script to run properly.


$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')
 INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';
 
 mysql_query($sql) or die (mysql_error());
 


Any help is much appreciated.



Answer:

You cannot execute two sql statements with mysql_query();
Use something like this:
$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')';
mysql_query($sql);

$clientId = mysql_insert_id();

$sql = 'INSERT INTO client_data (client_id, phone, zip, note) VALUES('.$clientId.', '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';

mysql_query($sql) or die (mysql_error());

read.csv appends/modifies column headdings with date values

Im trying to read a csv file into R that has date values in some of the colum headdings.


As an example, the data file looks something like this:


ID  Type   1/1/2001  2/1/2001  3/1/2001  4/1/2011
 A   Supply       25        35        45        55  
 B   Demand       26        35        41        22
 C   Supply       25        35        44        85  
 D   Supply       24        39        45        75  
 D   Demand       26        35        41        22
 


...and my read.csv logic looks like this


dat10<-read.csv("c:\data.csv",header=TRUE, sep=",",as.is=TRUE)
 


The read.csv works fine except it modifies the name of the colums with dates as follows:


x1.1.2001  x2.1.2001  x3.1.2001  x4.1.2001
 


Is there a way to prevent this, or a easy way to correct afterwards?



Answer:

Set check.names=FALSE. But be aware that 1/1/2001 et al are syntactically invalid names, therefore they may cause you some headaches.