0% found this document useful (0 votes)
60 views

06 Graphs

The document defines key concepts related to graphs including different types of graphs, graph representations, and common graph algorithms. It provides definitions for graphs, paths, cycles, directed vs undirected graphs, weighted graphs, and graph implementations using adjacency lists and matrices. It also summarizes common graph operations and algorithms like breadth-first search, depth-first search, finding cycles, connected components, minimal spanning trees, and single-source and all-pairs shortest path problems. Key algorithms discussed include Dijkstra's algorithm and Floyd's algorithm for shortest paths.

Uploaded by

api-3705606
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

06 Graphs

The document defines key concepts related to graphs including different types of graphs, graph representations, and common graph algorithms. It provides definitions for graphs, paths, cycles, directed vs undirected graphs, weighted graphs, and graph implementations using adjacency lists and matrices. It also summarizes common graph operations and algorithms like breadth-first search, depth-first search, finding cycles, connected components, minimal spanning trees, and single-source and all-pairs shortest path problems. Key algorithms discussed include Dijkstra's algorithm and Floyd's algorithm for shortest paths.

Uploaded by

api-3705606
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Definitions

• Graph – a set of N nodes and E edges (or arcs) where


each element of E is a pair of nodes
– Directed vs. undirected (ordered vs. unordered pairs)
Graphs – Weighted (each edge has an associated value/weight)
• Path in a directed graph
– List of nodes such that there is an arc from the ith to the i+1th
node for all 1 <=i<n
CSC 173 – Cost of the path is the sum of the costs on each arc
– A simple path visits no node more than once
• A cycle in a directed graph is a path of length >=1 that
begins and ends with the same node
– Cyclic graph – one with at least one cycle
– Acyclic graph – one with no cycles

Graph Implementations Operations on Graphs


• Adjacency lists • Breadth-first and depth-first search
– Is (u,v) an edge? – O(E/N) on average
– Successors(u) – O(E/N) on average • Finding cycles
– Predecessors (u) – O(N+E)
– Space – O(N+E)
• Connected components of undirected
– Best for sparse graphs (E << N2) graphs
• Minimal spanning tree: find a tree that
• Adjacency matrix
– Is (u,v) an edge? – O(1)
connects all nodes in a weighted graph
– Successors(u) – O(N) with minimal cost
– Predecessors(u) – O(N) • Single-source shortest path
– Space – O(N2)
– Best for dense graphs (E ~= N2) • All-pairs shortest path

Breadth-First Search Algorithm Depth-First Search Algorithm


BFS(vertex u) DFS(vertex u)
queue Q
u.marked = true u.marked = true
// perform required operation //perform required operation
Q.enqueue(u)
while not Q.empty()
for all neighbors v of u
v = Q.dequeue() if not v.marked
for all neighbors w of v
if not w.marked
DFS(v)
w.marked = true main
// perform required operation for all nodes u
Q.enqueue(w)
main u.marked = false
for all nodes u for all nodes u
u.marked = false
for all nodes u
if not u.marked
if not u.marked DFS(u)
BFS(u)

Running Time – O(N+E) Running Time – O(N+E)

1
Finding Cycles Post-Order DFS
Cycle_test_DFS(vertex u, p) //p is parent, needed only for undirected case postorder_DFS(vertex u, ref int nextnum)
u.marked = true; u.onpath = true;
//perform required operation
u.marked = true
for all neighbors v of u for all neighbors v of u
if v.onpath and (graph is directed or v != p) if not v.marked
announce cycle postorder_DFS(v, nextnum)
halt
if not v.marked u.ponum = nextnum++
cycle_test_DFS(v,u) postorder_main
u.onpath = false for all nodes u
main
for all nodes u
u.marked = false
u.marked = false; u.onpath = false int nextnum = 1
for all nodes u for all nodes u
if not u.marked if not u.marked
cycle_test_DFS(u, nil)
announce no cycle postorder_DFS(u, nextnum)

Running Time – O(N+E) Running Time – O(N+E)

Testing for Cycles: Alternative Topological Sort


cycle_test_alternate_main • Assign a linear ordering to the vertices in a
postorder_main() DAG such that if (i,j)is an edge, i appears
for all nodes u before j in the ordering
for all neighbors v of u – Use a stack to get the order right, pushing
if u.ponum <= v.ponum prior to exiting the DFS call
// = catches self-loops – Use the reverse of the postorder numbers to
announce cycle order nodes (there could be other sorts as
halt well due to unordered nodes)
announce no cycle

Single Source Shortest Path


Reachability
(SSSP)
• Given a directed graph G and a vertex v in
G, find all vertices in G that can be
reached from v by following arcs • Find the cost of the least cost path from a
– Set of nodes explored from v using depth-first source node v to each other node in G
search

2
Dijkstra’s Algorithm Dijkstra’s Algorithm
DijkstraSSSP(vertex u)
• Greedy algorithm for the SSSP problem vertex_set unsettled = V – {u}
for all nodes v //O(N)
• Abstractions used v.cost = infinity
for all neighbors v of u //O(N)
– Adjacency lists for neighbors of a node and v.cost = weight(u,v)
the cost of edges while not unsettled.empty() //O(N)
find v in unsettled s.t. v.cost is minimal //O(NlogN) if partially ordered tree
– Priority queue of nodes (“unsettled” nodes) for unsettled -= {v}
for all neighbors w of v O(E)
which the cheapest path has not yet been if v.cost + weight(v,w) < w.cost
identified //shorter path from u to w through v
w.cost = v.cost + weight(v,w)
– A notion of the lowest current known cost to unsettled.adjust() // re-order heap, O(ElogN)

each “unsettled” node

All Pairs Shortest Path –


Dijkstra’s Algorithm
Floyd’s Algorithm
• Uses adjacency matrix
• Why does it work? for u in 0..n-1
– On each iteration of the main loop, remove for v in 0..n01
vertex v with least cost from unsettled. v.cost // initialize with direct arcs
is the lowest cost path from u to v through D[u,v] = A[u,v] // A is infinity if there is no edge
known nodes. If there is a lower cost path for u in 0..n-1
for v in 0..n-1
through as yet unknown node x for w in 0..n-1
• x.cost would be less than v.cost if (D[v,u] + D[u,w] < D[v,w])
• x would be selected before v D[v,w] = D[v,u]+D[u,w]
• x would be in known
Running time – O(N*N*N)

Transitive Closure Minimum Cost Spanning Tree


(Warshall’s Algorithm) (MCST)
• Determine if there is a path between any two nodes • A graph G is connected if every pair of
for u in 0..n-1
for v in 0..n01
vertices is connected by a path
// there’s a path if there’s an edge • A spanning tree for G is a free (unrooted)
P[u,v] = A[u,v] // A is 0 if there is no edge tree that connects all vertices in G
for u in 0..n-1
for v in 0..n-1 • The cost of the spanning tree is the sum of
for w in 0..n-1 the cost of all edges in the tree
if (!P[v,w])
P[v,w] = P[v,u] && P[u,w]

3
Prim’s Algorithm for MCST Kruskal’s Algorithm for MCST
// Initially, one node in the spanning tree and no edges
PrimMCST(ref edge_set T) // T = set of edges in spanning tree
int closest[N] // closest[v] = vertex u in U closest to v
int lowcost[N] // lowcost[v] = weight[v,u]
// U is, implicitly, node 0 and the nodes connected to it by edges of T • Initially, each node is its own MCST
T = empty
for v in 1..N-1
• Merge two MCSTs at each step that can
lowcost[v] = weight[0,v]
closest[v] = 0 be connected together with the least cost
N-1 times do
// find the node closest to U and add it to U
min = lowcost[1]
adding the lowest cost edge
k=1
for j in 2..N-1 • Terminate when there is only 1 MCST that
if lowcost[j] < min
min = lowcost[j]
k=j
contains all vertices
//k is now the node outside U closest to something in U; add it to U
T += {(closest[k],k)} • Running time – O(ElogE)
lowcost[k] = infinity
for j in 1..N-1
if weight[k,j] < lowcost[j] && lowcost[j] < infinity
lowcost[j] = weight[k,j]
closest[j] = k

Other Operations of Interest


• Minimal graph coloring: assign a color to each node so
that no two nodes sharing an edge have the same color,
and the total number of distinct colors is as small as
possible
• Hamiltonian circuit: Find a cycle, if there is one, on which
every node appears exactly once
• Euler circuit: Find a cycle, if there is one, on which every
edge appears exactly once
• Traveling salesman problem (TSP): find a minimum-cost
cycle that visits every node exactly once

The above do not have known polynomial time solutions

You might also like