MIT1_204S10_lec07

# MIT1_204S10_lec07 - 1.204 Lecture 7 Data structures graphs...

This preview shows pages 1–6. Sign up to view the full content.

1.204 Lecture 7 Data structures: graphs, sets Graphs and Networks 0 1 0 A graph contains: Nodes Arcs, or pairs of nodes ij, i !=j 0 2 1 3 Graphs can be directed or undirected 1 Directed Undirected <0, 1> (0, 1) <1, 0> A network is a graph with a cost associated with each arc There are two kinds of networks in this world… Electrical and its kin…and traffic and its kin… 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Networks In an undirected network: Node i is adjacent to node j if arc ij exists Degree of node is number of arcs it terminates In a directed network: In-degree of node is number of arcs in Out-degree of node is number of arcs out 0 2 1 3 0 2 1 3 Out-degree of node 3= 4 In-degree of node 3= 0 3 0 1 2 4 Adjacency list representation of graphs Adjacency list of graph is n lists, one for each node i Adjacency list contains node(s) adjacent to i Arc Arc Arc the adjacent node list. Arc order arbitrary 0 2 1 3 1 2 3 null nl Head 0 1 2 2 null Node Node Node 4 null null 3 4 4 Head holds reference from each node i to 1 null 2
3 Adjacency array representation of graphs If no insertion/deletion of nodes and arcs is to be done (or graph is large), we dispense with the links and list. If we read the arcs from input and sort by ‘from’ node, we get: From To Cost (Arc number) 0 1 014 3 0 025 2 1 039 4 2 122 2 3 347 1 4 313 7 5 The ‘from’ node repeats when out-degree > 1 We recast this structure as arrays H, To, Cost: 2 3 4 (Node) H (Arc) To Cost 0 0 3 1 3 125 2 2 4 239 4 3 4 322 2 4 6 447 1 5 6 (sentinel) 5 1 37 The H array H[i] holds the index of the first arc out of node i Arcs must be sorted in order of origin (from) node Special case: If there are no arcs out of a node i, H[i]= H[i+1] This ensures that the inner for-loop below executes zero times in this special case for (int node= 0; node < nodes; node++) for (int arc= H[node]; arc < H[node+1]; arc++) System.out.println("Arc from node " + node + " to node "+ to[arc] + " cost " + cost[arc]); This is creating two entities, nodes and arcs And normalizing the data, which is a key principle

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The H array Node H Node 0 0 1 3 2 0 1 2 3 4 3 4 Arc From To 4 0 0 1 5 6 (sentinel) 1 0 2 2 0 3 3 1 2 Fill in the first arc out of each node 4 3 1 5 3 4 The H array Node H Node 0 0 1 3 2 4 3 4 4 6 5 6 (sentinel) Then set H[i]= H[i+1] for any nodes with no arcs out of them 0 1 2 3 4 Arc 0 0 1 1 0 2 2 0 3 3 1 2 4 3 1 5 3 4 4
Traversing a graph public class GraphSimple { public static void main(String[] args) { int[] H= {0, 3, 4, 4, 6, 6}; // 5 actual nodes int[] to= {1, 2, 3, 2, 4, 1}; // 6 arcs, numbers 0-5 int[] cost= {43, 52, 94, 22, 71, 37}; int nodes= H.length - 1; // Don't count sentinel 0 2 1 3 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

MIT1_204S10_lec07 - 1.204 Lecture 7 Data structures graphs...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online