0

I have solved TSP problem, now I have two output files.

File1. contains (x,y) value of all cities.

I have plotted this points using the following code:

d = read.table('city_data.txt',  sep=" ", col.names=c("cityX", "cityY"));

plot(d$cityX,d$cityY);

File2: contains connected nodes n sequence, suppose 25->22->4->21...........

in file city_data.txt row1 represents x,y value of node 1, row2 represents vale of node.....

how i can draw lines between 25->22->4->21...............

digEmAll
  • 56,430
  • 9
  • 115
  • 140
Sigcont
  • 717
  • 2
  • 8
  • 16
  • `plot(cityY~cityX,d[file2$nodes,]type="b")` should plot both points and lines in the order of `file2$node`. See [this post](http://stackoverflow.com/questions/27363653/find-shortest-path-from-x-y-coordinates/27386611#27386611). – jlhoward Dec 21 '14 at 15:46

1 Answers1

0

Here's a working example :

### Create a sample input data.frame
d <- read.csv(text=
'cityX,cityY
1.0,1.0
2.0,3.0
3.0,2.0
2.0,1.0
') 

# this line just add a row to close the TSP "tour" 
# (i.e. the salesman returning to the first city) 
# if you don't want it, just remove this line
d <- rbind(d,d[1,])
###

### PLOT 1 - LINES BETWEEN CITIES
# plot the cities and the lines connecting them
plot(x=d$cityX,y=d$cityY,type='b',xlab='X',ylab='Y')
###


### PLOT 2 - ARROWS BETWEEN CITIES
# plot the cities
plot(x=d$cityX,y=d$cityY,type='p',xlab='X',ylab='Y')
# plot the arrows connecting them
arrows(x0=head(d$cityX,-1),
       y0=head(d$cityY,-1),
       x1=tail(d$cityX,-1),
       y1=tail(d$cityY,-1))
###

PLOT 1:

PLOT 1

PLOT 2:

PLOT 2

digEmAll
  • 56,430
  • 9
  • 115
  • 140