g0 GRPHS Hanan Samet ompute Science epatment and ente fo utomation Reseach and Institute fo dvanced ompute Studies Univesity of Mayland ollege Pak, Mayland 0 e-mail: hjs@umiacs.umd.edu opyight 199 Hanan Samet hese notes may not e epoduced y any means (mechanical o electonic o any othe) without the expess witten pemission of Hanan Samet
GRPH (G) g1 Genealization of a tee 1. no longe a distinguished node called the oot implies no need to distinguish etween leaf and nonleaf nodes. two nodes can e linked y moe than one sequence of edges Fomally: set of vetices (V) and edges () joining them, with at most one edge joining any pai of vetices (V 0, V 1,, V n ): path of length n fom V 0 to V n (chain) Simple Path: distinct vetices (elementay chain) onnected: path etween any two vetices of G ycle: simple path of length 3 fom V 0 to V 0 (length in tems of edges) Plana: cuves intesect only at points of gaph egee: nume of edges intesecting at the node Isomophic: if thee is a one-to-one coespondence etween nodes and edges of two gaphs opyight 1998 y Hanan Samet
SMPL GRPH PROLM g Given n people at a paty who shake hands, show that at the paty s end, an even nume of people have shaken hands with an odd nume of people heoem: Fo any gaph G an even nume of nodes have an odd degee Poof: 1. each edge joins nodes. each edge contiutes to the sum of degees 3. sum of degees is even. thus an even nume of nodes with odd degee opyight 1998 y Hanan Samet
FR RS g3 onnected gaph with no cycles Given G as a fee tee with n vetices 1. onnected, ut not so if any edge is emoved. One simple path fom V to V ( V V ) 3. No cycles and n 1 edges. G is connected with n 1 edges F G iffeences fom egula tees: 1. No identification of oot. No distinction etween teminal and anch nodes opyight 1998 y Hanan Samet
FR SURS g efinition: set of edges such that all the vetices of the gaph ae connected to fom a fee tee x: distiution of telephone netwoks London pl l ln Pais p Rio de Janeio n New Yok p n uenos ies opyight 1998 y Hanan Samet
FR SURS g efinition: set of edges such that all the vetices of the gaph ae connected to fom a fee tee x: distiution of telephone netwoks London pl l ln Pais p Rio de Janeio n New Yok p n uenos ies Fee sutee opyight 1998 y Hanan Samet
FR SURS 3 z g efinition: set of edges such that all the vetices of the gaph ae connected to fom a fee tee x: distiution of telephone netwoks London pl l ln Pais p Rio de Janeio n New Yok p n uenos ies Fee sutee Given: connected gaph G n nodes () m edges (8) yclomatic Nume = nume of edges that must e deleted to yield a fee tee ( = m n + 1 ) opyight 1998 y Hanan Samet
IR GRPH g efinition: gaph with diection attached to the edges (V 0, V 1,, V n ): lementay path: path of length n fom V 0 to V n all vetices ae distinct icuit: cycle (ut can have length 1 o ) lementay icuit: all vetices ae distinct Indegee: Outdegee: Stongly onnected: path fom any V to any V Rooted: at least one V with paths to all V V Note: stongly connected implies ooted ut not vice vesa opyight 1998 y Hanan Samet
SRUURS FOR GRPHS g6 Must decide what infomation is to e accessile and with what ease Most impotant infomation conveyed y a gaph is connectivity which is indicated y its edges wo choices 1. vetex-ased keeps tack of nodes connected to each node can implement as aay of lists a. one enty fo each vetex p. [p] is a list of all vetices P that ae connected to p y vitue of the existence of an edge etween p and q whee q P (also known as an adjacency list). edge-ased keeps tack of edges usually epesented as a list of pais of fom (p q) whee thee is an edge etween vetices p and q dawack: need to seach entie set to detemine edges connected to a paticula vetex opyight 1998 y Hanan Samet
SRUURS FOR GRPHS g6 Must decide what infomation is to e accessile and with what ease Most impotant infomation conveyed y a gaph is connectivity which is indicated y its edges wo choices 1. vetex-ased keeps tack of nodes connected to each node can implement as aay of lists a. one enty fo each vetex p. [p] is a list of all vetices P that ae connected to p y vitue of the existence of an edge etween p and q whee q P (also known as an adjacency list). edge-ased keeps tack of edges usually epesented as a list of pais of fom (p q) whee thee is an edge etween vetices p and q dawack: need to seach entie set to detemine edges connected to a paticula vetex opyight 1998 y Hanan Samet
JNY MRIX g Hyid appoach Good fo epesenting a diected gaph ij = 1 if an edge exists fom i to j ij = 0 othewise = adjacency matix of distance Somewhat wasteful of space as thee is an enty fo evey possile edge even though the aay is usually spase 1. in such cases, a vetex-ased epesentation such as an adjacency list is moe economical. adjacency matix is useful if want to detect if an edge exists etween two vetices cumesome when using a list as need to seach Useful if want to keep tack of all vetices eachale fom evey vetex c d x: Leftmost deivations d c oolean matices 1 + 1 = 1 1 1 = 1 1 + 0 = 1 1 0 = 0 1 = 0 0 = 0 0 + 0 = 0 n ycle of length n ii = 1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 c 0 0 0 0 0 0 d 0 0 0 0 0 0 opyight 1998 y Hanan Samet
ONN GRPH g8 ef: thee exists path etween any two vetices of the gaph x: inay image 3 1 3 6 8 9 10 11 1 13 1 1 18 19 0 16 1 1 6 11 1 13 18 8 9 10 1 1 16 1 1. image gaph image elements ae vetices hoizontal and vetical adjacencies etween image elements ae edges. connected component laeling: detemine sepaate egions of inay image image gaph is stoed implicitly easy to access adjacent vetices given location of a vetex neithe a vetex-ased o edge-ased epesentation; instead algoithms ae ased on them 1. vetex-ased implies need to follow connectivity depth-fist o seed-filling appoach many page faults if disk-esident data. edge-ased detemines edges y examining image ow-y-ow only need to access two ows simultaneously good fo disk-esident data 3. oth take O(nume of image elements) time opyight 1998 y Hanan Samet 19 0
MINIMUM SPNNING R g9 ost ij associated with each edge fom i to j Find the fee sutee of G with minimum cost Solution: = connected nodes: initially { } U = unconnected nodes: initially { all nodes } 1. choose aitay node and place it in. select node in U that is closest to a node in and add edge; move node fom U to ; epeat until U is empty x: 1 3 1 Stat with node 0 is uilt y choosing: opyight 1998 y Hanan Samet
MINIMUM SPNNING R g9 ost ij associated with each edge fom i to j Find the fee sutee of G with minimum cost Solution: = connected nodes: initially { } U = unconnected nodes: initially { all nodes } 1. choose aitay node and place it in. select node in U that is closest to a node in and add edge; move node fom U to ; epeat until U is empty x: 1 3 1 Stat with node 0 is uilt y choosing: opyight 1998 y Hanan Samet
MINIMUM SPNNING R 3 z g9 ost ij associated with each edge fom i to j Find the fee sutee of G with minimum cost Solution: = connected nodes: initially { } U = unconnected nodes: initially { all nodes } 1. choose aitay node and place it in. select node in U that is closest to a node in and add edge; move node fom U to ; epeat until U is empty x: 1 3 1 Stat with node 0 is uilt y choosing: opyight 1998 y Hanan Samet
MINIMUM SPNNING R g 3 z g9 ost ij associated with each edge fom i to j Find the fee sutee of G with minimum cost Solution: = connected nodes: initially { } U = unconnected nodes: initially { all nodes } 1. choose aitay node and place it in. select node in U that is closest to a node in and add edge; move node fom U to ; epeat until U is empty x: 1 3 1 Stat with node 0 is uilt y choosing: 1 opyight 1998 y Hanan Samet
MINIMUM SPNNING R v g 3 z g9 ost ij associated with each edge fom i to j Find the fee sutee of G with minimum cost Solution: = connected nodes: initially { } U = unconnected nodes: initially { all nodes } 1. choose aitay node and place it in. select node in U that is closest to a node in and add edge; move node fom U to ; epeat until U is empty x: 1 3 1 Stat with node 0 is uilt y choosing: 1 3 opyight 1998 y Hanan Samet
MINIMUM SPNNING R 6 v g 3 z g9 ost ij associated with each edge fom i to j Find the fee sutee of G with minimum cost Solution: = connected nodes: initially { } U = unconnected nodes: initially { all nodes } 1. choose aitay node and place it in. select node in U that is closest to a node in and add edge; move node fom U to ; epeat until U is empty x: 1 3 1 Stat with node 0 is uilt y choosing: 1 3 1 opyight 1998 y Hanan Samet
MINIMUM SPNNING R z 6 v g 3 z g9 ost ij associated with each edge fom i to j Find the fee sutee of G with minimum cost Solution: = connected nodes: initially { } U = unconnected nodes: initially { all nodes } 1. choose aitay node and place it in. select node in U that is closest to a node in and add edge; move node fom U to ; epeat until U is empty x: 1 3 1 Stat with node 0 is uilt y choosing: 1 3 1 opyight 1998 y Hanan Samet
SHORS LMNRY HIN g10 Given node X0 in G find the shotest (cheapest) chain joining X0 with all the nodes of G Solution: = connected nodes: initially X0 U = unconnected nodes: initially all ut X0 = set of edges: initially empty 1. find the closest node in U to X0 (say X1). move X1 fom U to 3. add (X0, X1) to. fo each Xi in find Yi in U that is closest; choose Ym such that cost fom X0 to Ym is a minimum and add (Xm, Ym) to ; epeat until U is empty x: stat at node 0 1 3 1 Result: good fo designing flight schedules opyight 1998 y Hanan Samet
SHORS LMNRY HIN g10 Given node X0 in G find the shotest (cheapest) chain joining X0 with all the nodes of G Solution: = connected nodes: initially X0 U = unconnected nodes: initially all ut X0 = set of edges: initially empty 1. find the closest node in U to X0 (say X1). move X1 fom U to 3. add (X0, X1) to. fo each Xi in find Yi in U that is closest; choose Ym such that cost fom X0 to Ym is a minimum and add (Xm, Ym) to ; epeat until U is empty x: stat at node 0 1 3 1 Result: 0 (0,) good fo designing flight schedules opyight 1998 y Hanan Samet
SHORS LMNRY HIN 3 z g10 Given node X0 in G find the shotest (cheapest) chain joining X0 with all the nodes of G Solution: = connected nodes: initially X0 U = unconnected nodes: initially all ut X0 = set of edges: initially empty 1. find the closest node in U to X0 (say X1). move X1 fom U to 3. add (X0, X1) to. fo each Xi in find Yi in U that is closest; choose Ym such that cost fom X0 to Ym is a minimum and add (Xm, Ym) to ; epeat until U is empty x: stat at node 0 1 3 1 Result: 0 (0,) (0,) o (,) good fo designing flight schedules opyight 1998 y Hanan Samet
SHORS LMNRY HIN g 3 z g10 Given node X0 in G find the shotest (cheapest) chain joining X0 with all the nodes of G Solution: = connected nodes: initially X0 U = unconnected nodes: initially all ut X0 = set of edges: initially empty 1. find the closest node in U to X0 (say X1). move X1 fom U to 3. add (X0, X1) to. fo each Xi in find Yi in U that is closest; choose Ym such that cost fom X0 to Ym is a minimum and add (Xm, Ym) to ; epeat until U is empty x: stat at node 0 1 3 1 0 (0,) (0,) o (,) (,) Result: good fo designing flight schedules opyight 1998 y Hanan Samet
SHORS LMNRY HIN v g 3 z g10 Given node X0 in G find the shotest (cheapest) chain joining X0 with all the nodes of G Solution: = connected nodes: initially X0 U = unconnected nodes: initially all ut X0 = set of edges: initially empty 1. find the closest node in U to X0 (say X1). move X1 fom U to 3. add (X0, X1) to. fo each Xi in find Yi in U that is closest; choose Ym such that cost fom X0 to Ym is a minimum and add (Xm, Ym) to ; epeat until U is empty x: stat at node 0 1 3 1 0 (0,) (0,) o (,) (,) Result: (,) good fo designing flight schedules 3 opyight 1998 y Hanan Samet
SHORS LMNRY HIN 6 v g 3 z g10 Given node X0 in G find the shotest (cheapest) chain joining X0 with all the nodes of G Solution: = connected nodes: initially X0 U = unconnected nodes: initially all ut X0 = set of edges: initially empty 1. find the closest node in U to X0 (say X1). move X1 fom U to 3. add (X0, X1) to. fo each Xi in find Yi in U that is closest; choose Ym such that cost fom X0 to Ym is a minimum and add (Xm, Ym) to ; epeat until U is empty x: stat at node 0 1 3 1 0 (0,) (0,) o (,) (,) Result: (,) (,) o (,) good fo designing flight schedules 3 1 opyight 1998 y Hanan Samet
SHORS LMNRY HIN z 6 v g 3 z g10 Given node X0 in G find the shotest (cheapest) chain joining X0 with all the nodes of G Solution: = connected nodes: initially X0 U = unconnected nodes: initially all ut X0 = set of edges: initially empty 1. find the closest node in U to X0 (say X1). move X1 fom U to 3. add (X0, X1) to. fo each Xi in find Yi in U that is closest; choose Ym such that cost fom X0 to Ym is a minimum and add (Xm, Ym) to ; epeat until U is empty x: stat at node 0 1 3 1 Result: 0 (0,) (0,) o (,) (,) (,) (,) o (,) (,) good fo designing flight schedules 3 1 opyight 1998 y Hanan Samet
ULRIN HINS N YLS g11 When is it possile to tace a plana gaph without tacing any edge moe than once so that the pencil is neve emoved fom the pape? H I G F uleian cycle = edges ae all the edges of G (end up at point whee stated) heoem: an uleian cycle exists fo a connected gaph G wheneve all nodes have an even degee and vice vesa Poof: one diection: if an uleian cycle exists, then each time we ente a node y one edge we leave y anothe edge othe diection: moe complex uleian chain = joins nodes X and Y such that its edges ae all the edges of G (end up at point diffeent fom stating point) heoem: an uleian chain etween nodes X and Y fo a connected gaph G exists if and only if nodes X and Y have odd degee and the emaining nodes have even degee opyight 1998 y Hanan Samet
ULRIN HINS N YLS g11 When is it possile to tace a plana gaph without tacing any edge moe than once so that the pencil is neve emoved fom the pape? I H G F uleian chain uleian cycle Neithe uleian cycle = edges ae all the edges of G (end up at point whee stated) heoem: an uleian cycle exists fo a connected gaph G wheneve all nodes have an even degee and vice vesa Poof: one diection: if an uleian cycle exists, then each time we ente a node y one edge we leave y anothe edge othe diection: moe complex uleian chain = joins nodes X and Y such that its edges ae all the edges of G (end up at point diffeent fom stating point) heoem: an uleian chain etween nodes X and Y fo a connected gaph G exists if and only if nodes X and Y have odd degee and the emaining nodes have even degee opyight 1998 y Hanan Samet
HMILONIN HINS N YLS g1 When is it possile fo a salesman ased in city X to cove his teitoy in such a way that he neve visits a city moe than once, whee not evey city is connected diectly to anothe city? F F Hamiltonian cycle = cycle whee each vetex appeas once (salesman ends up at home!) Hamiltonian chain = chain whee each vetex appeas once (salesman need not end up at home!) Unlike uleian chains and cycles, no necessay and sufficient conditions exist fo a gaph G to have a Hamiltonian chain o cycle Sufficient condition: heoem: simple gaph with n 3 nodes such that fo any distinct nodes X and Y not joined y an edge and degee (X) + degee (Y) n, then G has a Hamiltonian cycle x: opyight 1998 y Hanan Samet
HMILONIN HINS N YLS g1 When is it possile fo a salesman ased in city X to cove his teitoy in such a way that he neve visits a city moe than once, whee not evey city is connected diectly to anothe city?? F F Hamiltonian cycle exists No Hamiltonian chain o cycle Hamiltonian chain exists (only one way fom to F) Hamiltonian cycle = F Hamiltonian cycle = cycle whee each vetex appeas once (salesman ends up at home!) Hamiltonian chain = chain whee each vetex appeas once (salesman need not end up at home!) Unlike uleian chains and cycles, no necessay and sufficient conditions exist fo a gaph G to have a Hamiltonian chain o cycle Sufficient condition: heoem: simple gaph with n 3 nodes such that fo any distinct nodes X and Y not joined y an edge and degee (X) + degee (Y) n, then G has a Hamiltonian cycle x: opyight 1998 y Hanan Samet