Algorithms and data structures
E N D
Presentation Transcript
Algorithmsand data structures Graph representation Selected problems
Graph 11 Directed Multiple edges 10 11 Loops 7 Weights Simple In more formal way: graph is an ordered pairG = (V, E) comprising a setV of vertices, nodes or points together with a set E of edges, arcs or lines, which are 2-element subsets of V
Graph representation 1 0 0 A 1 1 B 1 C D A B C D A B D A B C D Adjacency matrix C Adjacency lists B A C D B D B C • If matrix is symetrical (graph is undirected), we can store only a half of matrix.
Graph representation 0 1 0 1 m 1 0 1 1 n 0 1 0 1 o 1 1 1 0 p m n o p m p m n o p Incidence matrix n o Incidence lists n p m o p p n m n o
Weights representation 0 0 0 0 A 2 0 0 0 B 0 3 0 2 C 0 7 5 0 D A B C D A 2 7 B D A B C D Adjacency matrix 3 5 C 2 Adjacency lists A,2 D,2 B,3 C,5 B,7
Minimal Spanning Tree 2 4 2 3 1 8 5 6 2 3 4 6 Application:road building
Minimal Spanning Tree Kruskal’s algorithm: Pick the edge with the smallest possible weights and avoid cycles. Pot. problem: eficient cicle detection. Prim-Dijkstra’s algorithm: CurrentTree = edges with the smallest weight CurrentEdge = Pick the smallest edge incident with CurrentTree Add CurrentEdge to CurrentTree. Pot. problem: disconnected graph.
Shortest path 2 4 3 3 1 9 7 6 7 3 4 3 Applications: - shortest (fastest) road; - the least expensive (optimal) technology process.
Dijkstra’s Algorithm • A.: Weights are not less than 0 Starting vertex s: di = , Visited = s, ds = 0; For every vertex i adjacent to Visited di = e(s,i) Repeat until reach destination vertex (or there are unreached vertexes in general case): From V–Visited pick vertex j with smallest dj Visited = Visited + j For any neighbar i of j vertex from V–Visited update: di = min{ dj,di+e(j,i) }
Euler’s Cycle, Path Road – (open or not), that conatains all the edges from graph G Application: Chinese Postman ProblemDrawing/cutting with plotter aid.
Euler’s Cycle - Fleury’s alg. • Start froma vertex current, where deg(current) is odd (if such vertex exists) • while G contains any edge pick any edge d incident to current but avoid bridges • traverse the edge d and update current • remove d from G
Euler’s Cycle – stack algorithm • A: G is an Euler’s graph • current = Pick any vertex from G • while G contains edges and stack is not empty • if there exist any edge d incident with current • push current to the stack • traverse edge d and update current • remove edge d from G • else • move current to the solution pop current from the stack
Graph coloring vertices edges Colors are assigned while avoiding conflicts with neighbars Coloring
Coloring • Some interesting applications could be pointed i.e. • Assigning frequencies • Scheduling of lessons • Codes ressist for trasmission errors • Placing elements on curcuit board • Many more or less sophisticated models are studied • In general finding the optimal solution is very time-consuming so not optimal (but fast) algorithms are used
LF – heuristic (Largest First) • Pick an unpainted vertex with a max degree; • Assign minimal possible color. LF quality is linear i.e for any n there exist graph that will require n colors more than optimal solution. 2 1 2 3 1 1 2 2 1 2 1
SL – heuristic (Smallest Last) • Pick a vertex with smallest degree in remaining subgraph • Push vertex to the stack • Remove vertex (and incident edges) from graph • Color vertexes from the stack. ?
Similar problems could be diffienent • Euler cycle (visit all edges) is easy • Hamilton cycle (visit all vertices) is hard • Shortest path is easy • Longest path is hard