This preview shows page 1. Sign up to view the full content.
Unformatted text preview: PROJECT(4:(GRAPHS(AND(FILE(PROCESSING(
! INTRODUCTION( !
This!project!will!focus!on!file!processing.!The!idea!behind!file!processing!is!simple:!
information!that!appear!on!screen!and!data!that!are!given!manually!are!volatile!and!
prone!to!errors.!Would!you!prefer!having!to!key!in!1,000!values!when!instead!you!
can!store!them!all!in!a!file!and!read!them!from!there?!!
!
In!this!project,!we!will!see!a!data!structure!that!is!very!useful!in!a!series!of!
applications:!internet,!transportation,!medical,!social!etc.!A"graph."A!graph!in!
mathematics!is!a!series!of!vertices!and!edges,!where!each!edge!connects!two!
vertices.!A!directed!graph!more!specifically!contains!only!edges!that!can!start!from!a!
vertex!(tail)!and!arrive!to!another!vertex!(head).!!
! Figure!1.!An!example!of!an!undirected!graph!
! !
Figure!2.!An!example!of!a!directed!graph!
! ! !
! FILE(PROCESSING( !
Obviously,!inputting!a!number!of!edges!inside!by!hand!using!the!keyboard!will!be!a!
messy!procedure,!since!graphs!tend!to!have!more!than!100!edges!usually.!Instead,!
we!will!be!accepting!the!graph!from!within!a!file.!The!file!will!have!the!following!
format:!!
!
#of!edges!in!the!graph!
tail1!! head1! !
tail2! head2!
…!
…!
! OPTION(1:(Accepting(a(filename(from(the(user( !
Option!1!deals!with!the!errorKprone!part!of!the!user!giving!us!input!that!is!not!valid.!
In!this!part!you!will!have!to!ask!the!user!to!provide!you!with!the!location!of!the!file!
containing!the!graph.!Your!program!should!take!a!precaution!of!asking!again!for!the!
file!name!until!one!of!the!following!two!conditions!are!met:!!
1. the!file!opens!successfully,!
2. the!user!has!given!an!invalid!file!name!for!3!times.!
As!per!our!previous!assignments,!since!the!rest!of!the!options!require!a!file!to!have!
been!successfully!opened,!your!program!should!not!allow!for!any!of!the!other!
options!to!be!executed!before!this!one.!
! OPTION(2:(Reading(the(graph( !
Option!2!is!also!an!important!part!of!the!program,!that"needs"to"be"executed"
before"options"3,"4"and"5."At!this!part,!you!will!read!from!the!file!(whose!format!
you!already!know)!and!store!it!in!your!program!so!that!you!can!go!ahead!and!use!it!
in!the!next!options.!
! OPTIONS(3I4:(Calculating(number(of(arcs(from/to(node(i((( !
Options!3!and!4!are!very!similar!in!implementation.!The!user!provides!you!with!a!
node!(let!us!call!it!i)!and!then!your!program!must!calculate!how!many!arcs!are!
starting!from!that!node!leading!to!other!nodes!or!how!many!arcs!lead!to!the!node!i!
starting!from!other!nodes!in!the!graph.!
!
! OPTION(5:(( !
The!last!option!creates!an!output!file!where!the!user!can!find!a!subnetwork!of!nodes!
and!edges!from!the!original!network.!That!is,!if!the!user!provides!us!with!nodes!i!
and!k,!then!we!will!have!all!arcs!that!are!associated!with!i!and!k!in!the!subgraph!
obtained.!For!example,!let!us!consider!the!following!network:!!
! !
In!that!case!the!subnetwork!that!comprises!of!nodes!1!and!2!will!be:!!
!
! !
!
Let!us!see!a!sample!run!of!the!program.!
!
! SAMPLE(OUTPUT( !
Let!us!assume!that!in!the!file!grid.txt!we!find:!!
!
6!
1!2!
1!3!
1!4!
2!3!
2!5!
2!4!
!
That!implies!that!our!network!has:!!
• 6!arcs!in!total;!
• 3!arcs!leaving!node!1!and!going!to!nodes!2,!3!and!4!
• 3!arcs!leaving!node!2!and!going!to!nodes!3,!4!and!5!
!
!
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 2
No file has been given yet!
Graph successfully inputted! The graph has 0 arcs.
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 3
No file has been given yet!
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node. 4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 1
Please give the filename: grid.tst
Error opening file. Please try again.
Please give the filename: input.txt
Error opening file. Please try again.
Please give the filename: test.dat
Error opening file. Please try again.
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 1
Please give the filename: grid.txt
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 3
The file has not been read yet.
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 5
The file has not been read yet.
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 6 Wrong choice. Please choose again!
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 2
Graph successfully inputted! The graph has 6 arcs.
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 3
Please give the node you are looking for: 1
There are 3 arcs leaving node 1
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 3
Please give the node you are looking for: 1
There are 3 arcs leaving node 1
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 4
Please give the node you are looking for: 1
There are 0 arcs going to node 1
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node. 4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 4
Please give the node you are looking for: 5
There are 1 arcs going to node 5
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 5
Please give nodes you want to include in the subgraph (press 1
to stop input):1 1
Subgraph written successfully in subgraph.txt
COP2271 Data Manager
MAIN MENU
1. To input graph datafile.
2. To read graph from data file.
3. To compute the number of outgoing arcs from a certain node.
4. To compute the number of incoming arcs to a certain node.
5. To create a file with a subgraph.
Please choose: 0
Now quitting..
At the end of the execution, we find a file called “subgraph.txt”
that contains:
3
12
13
14 GOOD LUCK! ! ...
View Full
Document
 Fall '11
 Staff
 Computer Programming

Click to edit the document details