Download presentation
Presentation is loading. Please wait.
1
Algorithms and data structures
Graph representation Selected problems
2
Graph 11 Simple Directed Multiple edges 10 Loops 7 Weights
In more formal way: graph is an ordered pair G = (V, E) comprising a set V of vertices, nodes or points together with a set E of edges, arcs or lines, which are 2-element subsets of V
3
Graph representation Adjacency matrix Adjacency lists
B D A B C D Adjacency matrix A B C D 1 C Adjacency lists A B C D B A C D B D B C If matrix is symetrical (graph is undirected), we can store only a half of matrix.
4
Graph representation Incidence matrix Incidence lists m p m n o p n 1
1 o Incidence lists m n o p p n m o p p n m n o
5
Weights representation
2 7 B D A B C D Adjacency matrix 3 A B C D 5 7 2 3 5 C 2 Adjacency lists A B C D A,2 B,3 D,2 B,7 C,5
6
Connected graph, connected component
DFS
7
Minimal Spanning Tree 1 3 6 2 4 8 5 Application: road building
8
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.
9
Shortest path 1 3 6 7 4 9 2 Applications: - shortest (fastest) road; - the least expensive (optimal) technology process.
10
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) }
11
Euler’s Cycle, Path Road – (open or not), that conatains all the edges from graph G Application: Chinese Postman Problem Drawing/cutting with plotter aid.
12
Euler’s Cycle - Fleury’s alg.
Start from a 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
13
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
14
Graph coloring Coloring
Colors are assigned while avoiding conflicts with neighbars Coloring vertices edges
15
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
16
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. 1 2 2 3 1
17
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. ?
18
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.