It appears your problem has substantially altered. Please post a new question with all specifics. Are the relations fixed to those three? Do they have a cost metric? If multiple shortest paths exist?
Comment on Re^3: constructing a tree from csv file