How to connect line segments to form the least number of paths
You could solve this using any algorithm for the traveling salesman problem.  Construct a graph with one vertex per line.  Add an edge of cost 0 from line to another if they share an endpoint (these correspond to extending an existing path).  Add an edge of cost 1 between each other pair of lines (these correspond to starting a new path).  Now find the shortest traveling salesman tour of this graph.  That will correspond to a minimal set of paths that cover all of the lines; the number of lines will be equal to the weight of the traveling salesman tour.  There are standard algorithms and solvers for the TSP.