Saturday, 17 August 2013

time complexity for this algorithm

time complexity for this algorithm

Okay so I have caculated the time compleity to O(n^2):
Creat map of array lists-- O(n) create frequecy table---O(n) select groups
which have the highest frequency number---O(n) remove groups with highest
frequency number---0(n)
o(n)+ O(n^2) = o(n^2) have i calculated it correctly
when a friend check they said it was O(n^3) , I am unsure to why
/* * This program Finds a number which covers most groups. * Add this
number to solution group. Remove the groups that the number covers. *
repeat previous step until no group left. * All data is stored as a map of
array list. * A map of arraylists contains all groups. * Each array List
represents a group. * All elements within the array list are numbers who
represent the group. * */
public class Ass1_comp333 {
//Number of groups
public static void main(String[] args) throws IOException{
//Initialise map of array List
int g;
ArrayList<Integer> groupList; //An array of
ArrayList<Integer>solutionList= new ArrayList(); // Contains all
members who represent all groups
ArrayList<Integer>removalList= new ArrayList(); // A temporary list
which contains groups that have already been represented after an
Iteration
Map<Integer, ArrayList<Integer>> groupMap; //Stores Group Map. All the
input information
groupMap = new HashMap<Integer,ArrayList<Integer>>();
g = getDataFomConsole(groupMap); //Input Data from Key Board/ Piping
while(true){
//Find the element which is present in most groups
//System.out.println("GroupMap:" +groupMap);
int maxKey = mostCoverage(groupMap);
//remove all map Elements that has maxKey
for(int m : groupMap.keySet()){
if(groupMap.get(m).contains(maxKey)){
//add this to remove this entry
removalList.add(m);
//groupMap.remove(m);
}
}
solutionList.add(maxKey);
for( int m=0; m< removalList.size();m++){
groupMap.remove(removalList.get(m));
}
if(groupMap.isEmpty()) break;
}
//System.out.println("Solution List=" +solutionList);
System.out.println("The number of members selected and their ids are :");
//print minimal number of elements
System.out.println(solutionList.size());
//System.out.println(solutionList.toString());
//Print individual elements
for(int i=0;i<solutionList.size();i++){
System.out.print(solutionList.get(i)+" ");
}
System.out.println();
}
/*
* This method returns the member who covers the most number of groups
* It creates a new hashMap called frequencyTab. This hash map stores a
member
* as key and the number of groups it represents as value.
*
* groupMap is passed into this method as parameter. groupMap consists of
* list of groups who have not been represented yet. Each group is
represented
* as an arraylist along with members who represent it.
*
*
*/
private static int mostCoverage(Map<Integer, ArrayList<Integer>> groupMap){
//Create a map of members and the number of groups they represent
Map<Integer,Integer> frequencyTab = new HashMap<Integer, Integer>();
int maxKey=0,maxCount=0; //member who covers most groups and number of
groups covered
for(int m : groupMap.keySet()){
//For each group in list of groups yet unrepresented
for(int n=0;n< groupMap.get(m).size();n++){
//For each member who represents a particular group
if(frequencyTab.containsKey(groupMap.get(m).get(n))){
//check if this member has already represented a group
earlier
//If yes, find the number of groups it already represents
and add 1 to it
frequencyTab.put(groupMap.get(m).get(n),frequencyTab.get(groupMap.get(m).get(n))+1);
if(maxCount<frequencyTab.get(groupMap.get(m).get(n))){
//if this member representsthe most group, set this
member as max
maxKey= groupMap.get(m).get(n);
maxCount++;
}
}
else
{
//We have found a member who hasn't represented a single
group yet
//assign it as a new key and set value=1
frequencyTab.put(groupMap.get(m).get(n),1);
if(maxCount<frequencyTab.get(groupMap.get(m).get(n))){
maxKey= groupMap.get(m).get(n);
maxCount++;
}
}
}
}
//System.out.println("Frequency Tab=" +frequencyTab);
//maxKey has the highest presence
//System.out.println("Max Key=" +maxKey);
return maxKey;
}
private static int getDataFomConsole(Map<Integer, ArrayList<Integer>>
groupMap)throws IOException, NumberFormatException{
int g=0, nog; // store number of groups
Scanner s= null;
try{
s= new Scanner(new BufferedReader(new InputStreamReader(System.in)));
System.out.print("Enter the number of groups from which you must
find representatives: ");
//System.out.println("Entered Integer=" +s.next());
try{
g=Integer.parseInt(s.next());
System.out.println("Enter the list of members of each group (one
group per line, each terminated by 0):");
// Create map of array lists
for(int i=0;i<g;i++){
ArrayList<Integer> groupList = new ArrayList<Integer>();
groupMap.put(i,groupList);
}
nog=g;
int y=0;
while(nog>0){
int x= Integer.parseInt(s.next());
if (x==0) {
nog--;
y++;
} else {
groupMap.get(y).add(x);
}
}
} catch (NumberFormatException e) {
System.err.println("Not a valid Integer");
System.exit(1);
}
// Now construct the loop
}finally {
if (s!=null){
s.close();
}
}
//System.out.println(groupMap);
return g;
}
}

No comments:

Post a Comment