Optimization of Dynamic Ridesharing Systems