Graph Lecturer : Kritawan Siriboon, Room no. ext : ata Structures & lgorithm nalysis in, ++, Mark llen Weiss, ddison Wesley Graph G = (V, E) Graph efinitions V = set of vertices (nodes) E = set of edges (arcs)(lines) (arrows) E V = {,,, } E = {(,),(,),(,)} Order pairs (,E) = (E,) Order pairs (,) (,) irected graph (igraph) has directions associate with edges. Undirected graph has no direction associate with edges. KMIL 7 ata Structures & lgorithms Graph KMIL 7 ata Structures & lgorithms Graph onsecutive, djacent (oincident) Graph efinitions E wo edges of a directed graph are called consecutive if the head of the first one is at the notch of the second one. and are consecutive since they are are at the notch and at the head of an arrow. E wo edges of a graph are called adjacent (coincident) if they share a common vertex. and are adjacent to one another since they share a common edge. graph is a weighted graphif a number (weight) is assigned to each edge. Such weights might represent costs, lengths or capacities, etc. depending on the problem at hand. Path: sequence of nodes W, W, W,..., Wn when (W, W), (W, W),..., (Wn-, Wn) є E paths from to : and loop Path length = # of edges in a path length = unweigthed length = unweigthed length = + + = weigthed Loop: path of length from v to v ie. think that there is edge(v,v). KMIL 7 ata Structures & lgorithms Graph KMIL 7 ata Structures & lgorithms Graph
ycle, Simple Path yclein directed graph: path vertex!" vertex ##$%"& path length!'! ' and Simple path : path vertices #' vertex # vertex ("!) simple paths :. and not a simple path: Simple cycle : cycle of simple path. Ex.. ycle in undirected graph : edges!)"'+' edge &ie. path UVU)"',-. cycle /%(U,V) #$%(V,U) -. edge & cyclic Graph : no cycle irected cyclic Graph = G ==> ree KMIL 7 ata Structures & lgorithms Graph Undirected graph onnected VS isconnected onnected "& path vertex )- vertex "%%& isconnected irected graph onnected "& path vertex )- vertex "%%& Weakly onnected complete graph "& edge!",'! nodes KMIL 7 ata Structures & lgorithms Graph E E has indegree= has outdegree = Graph Examples irport System, raffic low,... Graph Representations 7 KMIL 7 ata Structures & lgorithms Graph 7 KMIL 7 ata Structures & lgorithms Graph
epth irst raversals redth irst raversals 7 7 7 7 epth irst raversal (Post Order),$ preorder ' / visit V V "&adjacent node &)"') visit +( visit +( #$ &+("' node &/ visit )-!: "! node &/ visit )"'"& adjacent node +( visit #$,'! $" visit adjacent node! node '!(&($!!' redth irst raversal (Level Order),$ traverse &$% level! ordered tree,! / visit V V "&adjacent node &)"') visit +( visit & adjacent " #$&)-!: node & visit )-"$ visit '!($ KMIL 7 ata Structures & lgorithms Graph KMIL 7 ata Structures & lgorithms Graph epth irst raversals depth_first ( void (*fp)( Vertex& ) ) const //<= ptr fp' print,.... init bool visited[mx]; = false for all vertices. visited 7.... for all un-visited vertex v // run for disconnected graph node traverse (v, visited, fp); // ' fp = &print(vertex&); KMIL 7 ata Structures & lgorithms Graph 7 traverse(vertex& v, bool visited[ ],void(*fp)(vertex&)) const. (*fp)(v); // run<=fp onv. visited[v] = true; // set v to be already visited. for all un-visited wthat adjacent to v traverse (w, visited, fp); 7 redth irst raversals bredth_first ( void (*fp)( Vertex& ) ) const //<= ptr fp' print,.... init bool visited[mx]; = false for all vertices. visited 7.... empty queue q;. for all un-visited vertex v // run for disconnected graph node q = enqueue(v) while (not empty q) w= q.dequeue() if (!visited[w]) visited[v] = true; // set v to be already visited 7 (*fp)(w); for all un-visited xthat adjacent wand xis not in q q.enqueue(x) KMIL 7 ata Structures & lgorithms Graph 7
Shortest Path v v v shortest weighted path : vto v= v,,,v cost = ++ = shortest unweighted path : vto v= v,,v cost = v Greedy lgorithm Greedy lgorithm : $!!&&&( stage -= #'&!)"' optimum '. #$(&?+()(&?!& quarter cents dime cents cents == > nikle cents penny cents suppose we have _cent_coin : So cents ->,,, (should be, ) KMIL 7 ata Structures & lgorithms Graph KMIL 7 ata Structures & lgorithms Graph v v v,v v v v,v KMIL 7 ata Structures & lgorithms Weighted Shortest Paths (ijkstra s algorithm) Greedy : for each current stage, choose the best. s.dist = ; for( ;;) v = smallest unknown dist. vertex if (v)"'"&) break; v.known = true; for each w adjacent to v )"' process if (w.dist > v.dist + weight(vw)) - w.dist -.,'+("'!',v,v v v v v,v v v v, v v,,,v (<),,,v,,v,,,,, (<), Graph v v v,v v v v,v Weighted Shortest Paths (ijkstra s algorithm),v,v,v v v v v v v v, v v,,,v, (<),,v,,v v,,,,, (<),,v v v,v v v v, v,,,v,,v,,,,7 KMIL 7 ata Structures & lgorithms Graph,v v v v,,,v,,
- - - v v v v - v- - v- - - - v known distance path - - - - - - 7 - KMIL 7 ata Structures & lgorithms v known distance path - - - - 7 - for all vertex v // algo. p fig v.dist = ; v.known = ; v.path = - ; s.dist = ; //s= vertex for (currist =; currist < NUMVERIES; currist ++) for each vertex v that!v.known && v.dist = currist v.known = ; //process#$ for each w & adjacent v #$% w.dist = w.dist = currentdist +; Graph 7 - - - v v v v - v- - v- - - - v v v v v v v v v v- v - - for all vertex v // algo. p fig v.dist = ; v.known = ; v.path = - ; s.dist = ; //s= vertex for (currist =; currist < NUMVERIES; currist ++) for each vertex v that!v.known && v.dist = currist v.known = ; //process#$ for each w & adjacent v #$% w.dist = w.dist = currentdist +; KMIL 7 ata Structures & lgorithms Graph v v v v v- v - v v v v v v v - v v v v v v v - v (using queue) Initial dequeued v dequeued v known dis p known dis p known dis p - - - v - - - - - v - - - - 7 - - - Q: vv vv v dequeued v dequeued dequeued v dequeued dequeued v known dis p known dis p known dis p known dis p known dis p v v v v v - - - - - v v v v v - v v v v 7 - - Q: v v v empty - - v v q.enqueue(s); - v - s.dist = ; while(!q.empty()) v = q.dequeue(); - - v.known = true; for each w adjacent to v & w.dis== infinity w.dist = w.dist +; q.enqueue(w); V KMIL 7 ata Structures & lgorithms Graph